1 /*
2 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_FREE_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or [email protected]
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58 /*
59 * File: task.h
60 * Author: Avadis Tevanian, Jr.
61 *
62 * This file contains the structure definitions for tasks.
63 *
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 [email protected] any
80 * improvements that they make and grant CSL redistribution rights.
81 *
82 */
83 /*
84 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
85 * support for mandatory and extensible security protections. This notice
86 * is included in support of clause 2.2 (b) of the Apple Public License,
87 * Version 2.0.
88 * Copyright (c) 2005 SPARTA, Inc.
89 */
90
91 #ifndef _KERN_TASK_H_
92 #define _KERN_TASK_H_
93
94 #include <kern/kern_types.h>
95 #include <kern/task_ref.h>
96 #include <mach/mach_types.h>
97 #include <sys/cdefs.h>
98
99 #ifdef XNU_KERNEL_PRIVATE
100 #include <kern/btlog.h>
101 #include <kern/kern_cdata.h>
102 #include <mach/sfi_class.h>
103 #include <kern/counter.h>
104 #include <kern/cs_blobs.h>
105 #include <kern/queue.h>
106 #include <kern/recount.h>
107 #include <sys/kern_sysctl.h>
108 #if CONFIG_EXCLAVES
109 #include <mach/exclaves.h>
110 #endif /* CONFIG_EXCLAVES */
111 #endif /* XNU_KERNEL_PRIVATE */
112
113 #ifdef MACH_KERNEL_PRIVATE
114 #include <mach/boolean.h>
115 #include <mach/port.h>
116 #include <mach/time_value.h>
117 #include <mach/message.h>
118 #include <mach/mach_param.h>
119 #include <mach/task_info.h>
120 #include <mach/exception_types.h>
121 #include <mach/vm_statistics.h>
122 #include <machine/task.h>
123
124 #include <kern/cpu_data.h>
125 #include <kern/queue.h>
126 #include <kern/exception.h>
127 #include <kern/locks.h>
128 #include <security/_label.h>
129 #include <ipc/ipc_port.h>
130
131 #include <kern/thread.h>
132 #include <mach/coalition.h>
133 #include <stdatomic.h>
134 #include <os/refcnt.h>
135
136 #if CONFIG_DEFERRED_RECLAIM
137 typedef struct vm_deferred_reclamation_metadata_s *vm_deferred_reclamation_metadata_t;
138 #endif /* CONFIG_DEFFERED_RECLAIM */
139
140 struct _cpu_time_qos_stats {
141 uint64_t cpu_time_qos_default;
142 uint64_t cpu_time_qos_maintenance;
143 uint64_t cpu_time_qos_background;
144 uint64_t cpu_time_qos_utility;
145 uint64_t cpu_time_qos_legacy;
146 uint64_t cpu_time_qos_user_initiated;
147 uint64_t cpu_time_qos_user_interactive;
148 };
149
150 struct task_writes_counters {
151 uint64_t task_immediate_writes;
152 uint64_t task_deferred_writes;
153 uint64_t task_invalidated_writes;
154 uint64_t task_metadata_writes;
155 };
156
157 struct task_pend_token {
158 union {
159 struct {
160 uint32_t tpt_update_sockets :1,
161 tpt_update_timers :1,
162 tpt_update_watchers :1,
163 tpt_update_live_donor :1,
164 tpt_update_coal_sfi :1,
165 tpt_update_throttle :1,
166 tpt_update_thread_sfi :1,
167 tpt_force_recompute_pri :1,
168 tpt_update_tg_ui_flag :1,
169 tpt_update_turnstile :1,
170 tpt_update_tg_app_flag :1,
171 tpt_update_game_mode :1,
172 tpt_update_carplay_mode :1;
173 };
174 uint32_t tpt_value;
175 };
176 };
177
178 typedef struct task_pend_token task_pend_token_s;
179 typedef struct task_pend_token *task_pend_token_t;
180
181 struct task_security_config {
182 union {
183 struct {
184 uint8_t hardened_heap: 1,
185 tpro :1,
186 reserved: 1;
187 };
188 uint8_t value;
189 };
190 };
191
192 typedef struct task_security_config task_security_config_s;
193
194 struct task_watchports;
195 #include <bank/bank_internal.h>
196
197 #ifdef MACH_BSD
198 struct proc;
199 struct proc_ro;
200 #endif
201
202 struct task {
203 /* Synchronization/destruction information */
204 decl_lck_mtx_data(, lock); /* Task's lock */
205 os_refcnt_t ref_count; /* Number of references to me */
206
207 #if DEVELOPMENT || DEBUG
208 struct os_refgrp *ref_group;
209 lck_spin_t ref_group_lock;
210 #endif /* DEVELOPMENT || DEBUG */
211
212 bool active; /* Task has not been terminated */
213 bool ipc_active; /* IPC with the task ports is allowed */
214 bool halting; /* Task is being halted */
215 bool message_app_suspended; /* Let iokit know when pidsuspended */
216
217 /* Virtual timers */
218 uint32_t vtimers;
219 uint32_t loadTag; /* dext ID used for logging identity */
220
221 /* Globally uniqueid to identify tasks and corpses */
222 uint64_t task_uniqueid;
223
224 /* Miscellaneous */
225 vm_map_t XNU_PTRAUTH_SIGNED_PTR("task.map") map; /* Address space description */
226 queue_chain_t tasks; /* global list of tasks */
227 struct task_watchports *watchports; /* watchports passed in spawn */
228 turnstile_inheritor_t returnwait_inheritor; /* inheritor for task_wait */
229
230 /* Threads in this task */
231 queue_head_t threads;
232 struct restartable_ranges *t_rr_ranges;
233
234 processor_set_t pset_hint;
235 struct affinity_space *affinity_space;
236
237 int thread_count;
238 uint32_t active_thread_count;
239 int suspend_count; /* Internal scheduling only */
240 #ifdef CONFIG_TASK_SUSPEND_STATS
241 struct task_suspend_stats_s t_suspend_stats; /* suspension statistics for this task */
242 task_suspend_source_array_t t_suspend_sources; /* array of suspender debug info for this task */
243 #endif /* CONFIG_TASK_SUSPEND_STATS */
244
245 /* User-visible scheduling information */
246 integer_t user_stop_count; /* outstanding stops */
247 integer_t legacy_stop_count; /* outstanding legacy stops */
248
249 int16_t priority; /* base priority for threads */
250 int16_t max_priority; /* maximum priority for threads */
251
252 integer_t importance; /* priority offset (BSD 'nice' value) */
253
254 #define task_is_immovable(task) \
255 !!(task_get_control_port_options(task) & TASK_CONTROL_PORT_IMMOVABLE)
256 #define task_is_pinned(task) \
257 !!(task_get_control_port_options(task) & TASK_CONTROL_PORT_PINNED)
258
259 /* Statistics */
260 uint64_t total_runnable_time;
261
262 struct recount_task tk_recount;
263
264 /* IPC structures */
265 decl_lck_mtx_data(, itk_lock_data);
266 /*
267 * Different flavors of task port.
268 * These flavors TASK_FLAVOR_* are defined in mach_types.h
269 */
270 struct ipc_port * XNU_PTRAUTH_SIGNED_PTR("task.itk_task_ports") itk_task_ports[TASK_SELF_PORT_COUNT];
271 #if CONFIG_CSR
272 struct ipc_port * XNU_PTRAUTH_SIGNED_PTR("task.itk_settable_self") itk_settable_self; /* a send right */
273 #endif /* CONFIG_CSR */
274 struct ipc_port * XNU_PTRAUTH_SIGNED_PTR("task.itk_self") itk_self; /* immovable/pinned task port, does not hold right */
275 struct exception_action exc_actions[EXC_TYPES_COUNT];
276 /* special exception port used by task_register_hardened_exception_handler */
277 struct hardened_exception_action hardened_exception_action;
278 /* a send right each valid element */
279 struct ipc_port * XNU_PTRAUTH_SIGNED_PTR("task.itk_host") itk_host; /* a send right */
280 struct ipc_port * XNU_PTRAUTH_SIGNED_PTR("task.itk_bootstrap") itk_bootstrap; /* a send right */
281 struct ipc_port * XNU_PTRAUTH_SIGNED_PTR("task.itk_debug_control") itk_debug_control; /* send right for debugmode communications */
282 struct ipc_port * XNU_PTRAUTH_SIGNED_PTR("task.itk_task_access") itk_task_access; /* and another send right */
283 struct ipc_port * XNU_PTRAUTH_SIGNED_PTR("task.itk_resume") itk_resume; /* a receive right to resume this task */
284 struct ipc_port * XNU_PTRAUTH_SIGNED_PTR("task.itk_registered") itk_registered[TASK_PORT_REGISTER_MAX];
285 /* all send rights */
286 ipc_port_t * XNU_PTRAUTH_SIGNED_PTR("task.itk_dyld_notify") itk_dyld_notify; /* lazy send rights array of size DYLD_MAX_PROCESS_INFO_NOTIFY_COUNT */
287 #if CONFIG_PROC_RESOURCE_LIMITS
288 struct ipc_port * XNU_PTRAUTH_SIGNED_PTR("task.itk_resource_notify") itk_resource_notify; /* a send right to the resource notify port */
289 #endif /* CONFIG_PROC_RESOURCE_LIMITS */
290 struct ipc_space * XNU_PTRAUTH_SIGNED_PTR("task.itk_space") itk_space;
291
292 ledger_t ledger;
293 /* Synchronizer ownership information */
294 queue_head_t semaphore_list; /* list of owned semaphores */
295 int semaphores_owned; /* number of semaphores owned */
296
297 unsigned int priv_flags; /* privilege resource flags */
298 #define VM_BACKING_STORE_PRIV 0x1
299
300 MACHINE_TASK
301
302 counter_t faults; /* faults counter */
303 counter_t pageins; /* pageins counter */
304 counter_t cow_faults; /* copy on write fault counter */
305 counter_t messages_sent; /* messages sent counter */
306 counter_t messages_received; /* messages received counter */
307 counter_t pages_grabbed; /* pages grabbed */
308 counter_t pages_grabbed_kern; /* pages grabbed (kernel) */
309 counter_t pages_grabbed_iopl; /* pages grabbed (iopl) */
310 counter_t pages_grabbed_upl; /* pages grabbed (upl) */
311 uint32_t decompressions; /* decompression counter (from threads that already terminated) */
312 uint32_t syscalls_mach; /* mach system call counter */
313 uint32_t syscalls_unix; /* unix system call counter */
314 uint32_t c_switch; /* total context switches */
315 uint32_t p_switch; /* total processor switches */
316 uint32_t ps_switch; /* total pset switches */
317
318 #ifdef MACH_BSD
319 struct proc_ro * bsd_info_ro;
320 #endif
321 kcdata_descriptor_t corpse_info;
322 uint64_t crashed_thread_id;
323 queue_chain_t corpse_tasks;
324 #ifdef CONFIG_MACF
325 struct label * crash_label;
326 #endif
327 volatile uint32_t t_flags; /* general-purpose task flags protected by task_lock (TL) */
328 #define TF_NONE 0
329 #define TF_64B_ADDR 0x00000001 /* task has 64-bit addressing */
330 #define TF_64B_DATA 0x00000002 /* task has 64-bit data registers */
331 #define TF_CPUMON_WARNING 0x00000004 /* task has at least one thread in CPU usage warning zone */
332 #define TF_WAKEMON_WARNING 0x00000008 /* task is in wakeups monitor warning zone */
333 #define TF_TELEMETRY (TF_CPUMON_WARNING | TF_WAKEMON_WARNING) /* task is a telemetry participant */
334 #define TF_GPU_DENIED 0x00000010 /* task is not allowed to access the GPU */
335 #define TF_PENDING_CORPSE 0x00000040 /* task corpse has not been reported yet */
336 #define TF_CORPSE_FORK 0x00000080 /* task is a forked corpse */
337 #define TF_CA_CLIENT_WI 0x00000800 /* task has CA_CLIENT work interval */
338 #define TF_DARKWAKE_MODE 0x00001000 /* task is in darkwake mode */
339 #define TF_NO_SMT 0x00002000 /* task threads must not be paired with SMT threads */
340 #define TF_SYS_VERSION_COMPAT 0x00008000 /* shim task accesses to OS version data (macOS - app compatibility) */
341 #define TF_TECS 0x00020000 /* task threads must enable CPU security */
342 #if defined(__x86_64__)
343 #define TF_INSN_COPY_OPTOUT 0x00040000 /* task threads opt out of unhandled-fault instruction stream collection */
344 #endif
345 #define TF_COALITION_MEMBER 0x00080000 /* task is a member of a coalition */
346 #define TF_NO_CORPSE_FORKING 0x00100000 /* do not fork a corpse for this task */
347 #define TF_USE_PSET_HINT_CLUSTER_TYPE 0x00200000 /* bind task to task->pset_hint->pset_cluster_type */
348 #define TF_DYLD_ALL_IMAGE_FINAL 0x00400000 /* all_image_info_addr can no longer be changed */
349 #define TF_HASPROC 0x00800000 /* task points to a proc */
350 #define TF_HAS_REPLY_PORT_TELEMETRY 0x10000000 /* Rate limit telemetry for reply port security semantics violations rdar://100244531 */
351 #define TF_HAS_PROVISIONAL_REPLY_PORT_TELEMETRY 0x20000000 /* Rate limit telemetry for creating provisional reply port rdar://136996362 */
352 #define TF_GAME_MODE 0x40000000 /* Set the game mode bit for CLPC */
353 #define TF_CARPLAY_MODE 0x80000000 /* Set the carplay mode bit for CLPC */
354
355 /*
356 * WARNING: These TF_ and TFRO_ flags are NOT automatically inherited by a child of fork
357 * If you believe something should be inherited, you must manually inherit the flags in `task_create_internal`
358 */
359
360 /*
361 * RO-protected flags:
362 */
363 #define TFRO_CORPSE 0x00000020 /* task is a corpse */
364 #define TFRO_HARDENED 0x00000100 /* task is a hardened runtime binary */
365 #if XNU_TARGET_OS_OSX
366 #define TFRO_MACH_HARDENING_OPT_OUT 0x00000200 /* task might load third party plugins on macOS and should be opted out of mach hardening */
367 #endif /* XNU_TARGET_OS_OSX */
368 #define TFRO_PLATFORM 0x00000400 /* task is a platform binary */
369 #define TFRO_FILTER_MSG 0x00004000 /* task calls into message filter callback before sending a message */
370 #define TFRO_PAC_EXC_FATAL 0x00010000 /* task is marked a corpse if a PAC exception occurs */
371 #define TFRO_JIT_EXC_FATAL 0x00020000 /* kill the task on access violations from privileged JIT code */
372 #define TFRO_PAC_ENFORCE_USER_STATE 0x01000000 /* Enforce user and kernel signed thread state */
373 #if CONFIG_EXCLAVES
374 #define TFRO_HAS_KD_ACCESS 0x02000000 /* Access to the kernel exclave resource domain */
375 #endif /* CONFIG_EXCLAVES */
376 #define TFRO_FREEZE_EXCEPTION_PORTS 0x04000000 /* Setting new exception ports on the task/thread is disallowed */
377 #if CONFIG_EXCLAVES
378 #define TFRO_HAS_SENSOR_MIN_ON_TIME_ACCESS 0x08000000 /* Access to sensor minimum on time call */
379 #endif /* CONFIG_EXCLAVES */
380
381 /*
382 * Task is running within a 64-bit address space.
383 */
384 #define task_has_64Bit_addr(task) \
385 (((task)->t_flags & TF_64B_ADDR) != 0)
386 #define task_set_64Bit_addr(task) \
387 ((task)->t_flags |= TF_64B_ADDR)
388 #define task_clear_64Bit_addr(task) \
389 ((task)->t_flags &= ~TF_64B_ADDR)
390
391 /*
392 * Task is using 64-bit machine state.
393 */
394 #define task_has_64Bit_data(task) \
395 (((task)->t_flags & TF_64B_DATA) != 0)
396 #define task_set_64Bit_data(task) \
397 ((task)->t_flags |= TF_64B_DATA)
398 #define task_clear_64Bit_data(task) \
399 ((task)->t_flags &= ~TF_64B_DATA)
400
401 #define task_corpse_pending_report(task) \
402 (((task)->t_flags & TF_PENDING_CORPSE) != 0)
403
404 #define task_set_corpse_pending_report(task) \
405 ((task)->t_flags |= TF_PENDING_CORPSE)
406
407 #define task_clear_corpse_pending_report(task) \
408 ((task)->t_flags &= ~TF_PENDING_CORPSE)
409
410 #define task_is_a_corpse_fork(task) \
411 (((task)->t_flags & TF_CORPSE_FORK) != 0)
412
413 #define task_set_coalition_member(task) \
414 ((task)->t_flags |= TF_COALITION_MEMBER)
415
416 #define task_clear_coalition_member(task) \
417 ((task)->t_flags &= ~TF_COALITION_MEMBER)
418
419 #define task_is_coalition_member(task) \
420 (((task)->t_flags & TF_COALITION_MEMBER) != 0)
421
422 #define task_has_proc(task) \
423 (((task)->t_flags & TF_HASPROC) != 0)
424
425 #define task_set_has_proc(task) \
426 ((task)->t_flags |= TF_HASPROC)
427
428 #define task_clear_has_proc(task) \
429 ((task)->t_flags &= ~TF_HASPROC)
430
431 #define task_has_reply_port_telemetry(task) \
432 (((task)->t_flags & TF_HAS_REPLY_PORT_TELEMETRY) != 0)
433
434 #define task_set_reply_port_telemetry(task) \
435 ((task)->t_flags |= TF_HAS_REPLY_PORT_TELEMETRY)
436
437 #define task_has_provisional_reply_port_telemetry(task) \
438 (((task)->t_flags & TF_HAS_PROVISIONAL_REPLY_PORT_TELEMETRY) != 0)
439
440 #define task_set_provisional_reply_port_telemetry(task) \
441 ((task)->t_flags |= TF_HAS_PROVISIONAL_REPLY_PORT_TELEMETRY)
442
443 uint32_t t_procflags; /* general-purpose task flags protected by proc_lock (PL) */
444 #define TPF_NONE 0
445 #define TPF_DID_EXEC 0x00000001 /* task has been execed to a new task */
446 #define TPF_EXEC_COPY 0x00000002 /* task is the new copy of an exec */
447
448 #define task_did_exec_internal(task) \
449 (((task)->t_procflags & TPF_DID_EXEC) != 0)
450
451 #define task_is_exec_copy_internal(task) \
452 (((task)->t_procflags & TPF_EXEC_COPY) != 0)
453
454 mach_vm_address_t all_image_info_addr; /* dyld __all_image_info */
455 mach_vm_size_t all_image_info_size; /* section location and size */
456
457 #if CONFIG_CPU_COUNTERS
458 #define TASK_KPC_FORCED_ALL_CTRS 0x2 /* Bit in "t_kpc" signifying this task forced all counters */
459 uint32_t t_kpc; /* kpc flags */
460 #endif /* CONFIG_CPU_COUNTERS */
461
462 bool pidsuspended; /* pid_suspend called; no threads can execute */
463 bool frozen; /* frozen; private resident pages committed to swap */
464 bool changing_freeze_state; /* in the process of freezing or thawing */
465 bool is_large_corpse;
466 uint16_t policy_ru_cpu :4,
467 policy_ru_cpu_ext :4,
468 applied_ru_cpu :4,
469 applied_ru_cpu_ext :4;
470 uint8_t rusage_cpu_flags;
471 uint8_t rusage_cpu_percentage; /* Task-wide CPU limit percentage */
472 uint8_t rusage_cpu_perthr_percentage; /* Per-thread CPU limit percentage */
473 #if MACH_ASSERT
474 int8_t suspends_outstanding; /* suspends this task performed in excess of resumes */
475 #endif
476 uint8_t t_returnwaitflags;
477 #define TWF_NONE 0
478 #define TRW_LRETURNWAIT 0x01 /* task is waiting for fork/posix_spawn/exec to complete */
479 #define TRW_LRETURNWAITER 0x02 /* task is waiting for TRW_LRETURNWAIT to get cleared */
480 #define TRW_LEXEC_COMPLETE 0x04 /* thread should call exec complete */
481
482 #if CONFIG_EXCLAVES
483 uint8_t t_exclave_state;
484 #define TES_NONE 0
485 #define TES_CONCLAVE_TAINTED 0x01 /* Task has talked to conclave, xnu has tainted the process */
486 #define TES_CONCLAVE_UNTAINTABLE 0x02 /* Task can not be tainted by xnu when it talks to conclave */
487 #endif /* CONFIG_EXCLAVES */
488
489 #if __has_feature(ptrauth_calls)
490 bool shared_region_auth_remapped; /* authenticated sections ready for use */
491 char *shared_region_id; /* determines which ptr auth key to use */
492 #endif /* __has_feature(ptrauth_calls) */
493 struct vm_shared_region *shared_region;
494
495 uint64_t rusage_cpu_interval; /* Task-wide CPU limit interval */
496 uint64_t rusage_cpu_perthr_interval; /* Per-thread CPU limit interval */
497 uint64_t rusage_cpu_deadline;
498 thread_call_t rusage_cpu_callt;
499 #if CONFIG_TASKWATCH
500 queue_head_t task_watchers; /* app state watcher threads */
501 int num_taskwatchers;
502 int watchapplying;
503 #endif /* CONFIG_TASKWATCH */
504
505 struct bank_task *bank_context; /* pointer to per task bank structure */
506
507 #if IMPORTANCE_INHERITANCE
508 struct ipc_importance_task *task_imp_base; /* Base of IPC importance chain */
509 #endif /* IMPORTANCE_INHERITANCE */
510
511 vm_extmod_statistics_data_t extmod_statistics;
512
513 struct task_requested_policy requested_policy;
514 struct task_effective_policy effective_policy;
515
516 struct task_pend_token pended_coalition_changes;
517
518 /*
519 * Can be merged with imp_donor bits, once the IMPORTANCE_INHERITANCE macro goes away.
520 */
521 uint32_t low_mem_notified_warn :1, /* warning low memory notification is sent to the task */
522 low_mem_notified_critical :1, /* critical low memory notification is sent to the task */
523 purged_memory_warn :1, /* purgeable memory of the task is purged for warning level pressure */
524 purged_memory_critical :1, /* purgeable memory of the task is purged for critical level pressure */
525 low_mem_privileged_listener :1, /* if set, task would like to know about pressure changes before other tasks on the system */
526 mem_notify_reserved :27; /* reserved for future use */
527
528 uint32_t memlimit_is_active :1, /* if set, use active attributes, otherwise use inactive attributes */
529 memlimit_is_fatal :1, /* if set, exceeding current memlimit will prove fatal to the task */
530 memlimit_active_exc_resource :1, /* if set, suppress exc_resource exception when task exceeds active memory limit */
531 memlimit_inactive_exc_resource :1, /* if set, suppress exc_resource exception when task exceeds inactive memory limit */
532 memlimit_attrs_reserved :28; /* reserved for future use */
533
534 io_stat_info_t task_io_stats;
535
536 struct task_writes_counters task_writes_counters_internal;
537 struct task_writes_counters task_writes_counters_external;
538
539 /*
540 * The cpu_time_qos_stats fields are protected by the task lock
541 */
542 struct _cpu_time_qos_stats cpu_time_eqos_stats;
543 struct _cpu_time_qos_stats cpu_time_rqos_stats;
544
545 /* Statistics accumulated for terminated threads from this task */
546 uint32_t task_timer_wakeups_bin_1;
547 uint32_t task_timer_wakeups_bin_2;
548 uint64_t task_gpu_ns;
549
550 uint8_t task_can_transfer_memory_ownership;
551 #if DEVELOPMENT || DEBUG
552 uint8_t task_no_footprint_for_debug;
553 #endif
554 uint8_t task_objects_disowning;
555 uint8_t task_objects_disowned;
556 /* # of purgeable volatile VM objects owned by this task: */
557 int task_volatile_objects;
558 /* # of purgeable but not volatile VM objects owned by this task: */
559 int task_nonvolatile_objects;
560 int task_owned_objects;
561 queue_head_t task_objq;
562 decl_lck_mtx_data(, task_objq_lock); /* protects "task_objq" */
563
564 unsigned int task_thread_limit:16;
565 #if __arm64__
566 unsigned int task_legacy_footprint:1;
567 unsigned int task_extra_footprint_limit:1;
568 unsigned int task_ios13extended_footprint_limit:1;
569 #endif /* __arm64__ */
570 unsigned int task_region_footprint:1;
571 unsigned int task_region_info_flags:1;
572 unsigned int task_has_crossed_thread_limit:1;
573 unsigned int task_rr_in_flight:1; /* a t_rr_synchronzie() is in flight */
574 /*
575 * A task's coalition set is "adopted" in task_create_internal
576 * and unset in task_deallocate_internal, so each array member
577 * can be referenced without the task lock.
578 * Note: these fields are protected by coalition->lock,
579 * not the task lock.
580 */
581 coalition_t coalition[COALITION_NUM_TYPES];
582 queue_chain_t task_coalition[COALITION_NUM_TYPES];
583 uint64_t dispatchqueue_offset;
584
585 #if DEVELOPMENT || DEBUG
586 boolean_t task_unnested;
587 int task_disconnected_count;
588 #endif
589
590 #if HYPERVISOR
591 void * XNU_PTRAUTH_SIGNED_PTR("task.hv_task_target") hv_task_target; /* hypervisor virtual machine object associated with this task */
592 #endif /* HYPERVISOR */
593
594 #if CONFIG_SECLUDED_MEMORY
595 uint8_t task_can_use_secluded_mem;
596 uint8_t task_could_use_secluded_mem;
597 uint8_t task_could_also_use_secluded_mem;
598 uint8_t task_suppressed_secluded;
599 #endif /* CONFIG_SECLUDED_MEMORY */
600
601 task_exc_guard_behavior_t task_exc_guard;
602 mach_vm_address_t mach_header_vm_address;
603
604 queue_head_t io_user_clients;
605
606 #if CONFIG_FREEZE
607 queue_head_t task_frozen_cseg_q; /* queue of csegs frozen to NAND */
608 #endif /* CONFIG_FREEZE */
609 boolean_t donates_own_pages; /* pages land on the special Q (only swappable pages on iPadOS, early swap on macOS) */
610 uint32_t task_shared_region_slide; /* cached here to avoid locking during telemetry */
611 #if CONFIG_PHYS_WRITE_ACCT
612 uint64_t task_fs_metadata_writes;
613 #endif /* CONFIG_PHYS_WRITE_ACCT */
614 uuid_t task_shared_region_uuid;
615 #if CONFIG_MEMORYSTATUS
616 uint64_t memstat_dirty_start; /* last abstime transition into the dirty band or last call to task_ledger_settle_dirty_time while dirty */
617 #endif /* CONFIG_MEMORYSTATUS */
618 vmobject_list_output_t corpse_vmobject_list;
619 uint64_t corpse_vmobject_list_size;
620 #if CONFIG_DEFERRED_RECLAIM
621 vm_deferred_reclamation_metadata_t deferred_reclamation_metadata; /* Protected by the task lock */
622 #endif /* CONFIG_DEFERRED_RECLAIM */
623
624 #if CONFIG_EXCLAVES
625 void * XNU_PTRAUTH_SIGNED_PTR("task.conclave") conclave;
626 void * XNU_PTRAUTH_SIGNED_PTR("task.exclave_crash_info") exclave_crash_info;
627 uint32_t exclave_crash_info_length;
628 #endif /* CONFIG_EXCLAVES */
629
630 /* Auxiliary code-signing information */
631 uint64_t task_cs_auxiliary_info;
632
633 /* Runtime security mitigations */
634 task_security_config_s security_config;
635 };
636
637 ZONE_DECLARE_ID(ZONE_ID_PROC_TASK, void *);
638 extern zone_t proc_task_zone;
639
640 extern task_control_port_options_t task_get_control_port_options(task_t task);
641 extern void task_set_control_port_options(task_t task, task_control_port_options_t opts);
642
643 /*
644 * EXC_GUARD default delivery behavior for optional Mach port and VM guards.
645 * Applied to new tasks at creation time.
646 */
647 extern task_exc_guard_behavior_t task_exc_guard_default;
648 extern size_t proc_and_task_size;
649 extern void *get_bsdtask_info(task_t t);
650 extern void *task_get_proc_raw(task_t task);
651 static inline void
task_require(struct task * task)652 task_require(struct task *task)
653 {
654 zone_id_require(ZONE_ID_PROC_TASK, proc_and_task_size, task_get_proc_raw(task));
655 }
656
657 #define task_lock(task) lck_mtx_lock(&(task)->lock)
658 #define task_lock_assert_owned(task) LCK_MTX_ASSERT(&(task)->lock, LCK_MTX_ASSERT_OWNED)
659 #define task_lock_try(task) lck_mtx_try_lock(&(task)->lock)
660 #define task_unlock(task) lck_mtx_unlock(&(task)->lock)
661
662 #define task_objq_lock_init(task) lck_mtx_init(&(task)->task_objq_lock, &vm_object_lck_grp, &vm_object_lck_attr)
663 #define task_objq_lock_destroy(task) lck_mtx_destroy(&(task)->task_objq_lock, &vm_object_lck_grp)
664 #define task_objq_lock(task) lck_mtx_lock(&(task)->task_objq_lock)
665 #define task_objq_lock_assert_owned(task) LCK_MTX_ASSERT(&(task)->task_objq_lock, LCK_MTX_ASSERT_OWNED)
666 #define task_objq_lock_try(task) lck_mtx_try_lock(&(task)->task_objq_lock)
667 #define task_objq_unlock(task) lck_mtx_unlock(&(task)->task_objq_lock)
668
669 #define itk_lock_init(task) lck_mtx_init(&(task)->itk_lock_data, &ipc_lck_grp, &ipc_lck_attr)
670 #define itk_lock_destroy(task) lck_mtx_destroy(&(task)->itk_lock_data, &ipc_lck_grp)
671 #define itk_lock(task) lck_mtx_lock(&(task)->itk_lock_data)
672 #define itk_unlock(task) lck_mtx_unlock(&(task)->itk_lock_data)
673
674 /* task clear return wait flags */
675 #define TCRW_CLEAR_INITIAL_WAIT 0x1
676 #define TCRW_CLEAR_FINAL_WAIT 0x2
677 #define TCRW_CLEAR_EXEC_COMPLETE 0x4
678 #define TCRW_CLEAR_ALL_WAIT (TCRW_CLEAR_INITIAL_WAIT | TCRW_CLEAR_FINAL_WAIT)
679
680 /* Initialize task module */
681 extern void task_init(void);
682
683 /* coalition_init() calls this to initialize ledgers before task_init() */
684 extern void init_task_ledgers(void);
685
686 extern task_t current_task(void) __pure2;
687
688 extern bool task_is_driver(task_t task);
689 extern uint32_t task_ro_flags_get(task_t task);
690 extern void task_ro_flags_set(task_t task, uint32_t flags);
691 extern void task_ro_flags_clear(task_t task, uint32_t flags);
692
693 extern lck_attr_t task_lck_attr;
694 extern lck_grp_t task_lck_grp;
695
696 struct task_watchport_elem {
697 task_t twe_task;
698 ipc_port_t twe_port; /* (Space lock) */
699 ipc_port_t XNU_PTRAUTH_SIGNED_PTR("twe_pdrequest") twe_pdrequest;
700 };
701
702 struct task_watchports {
703 os_refcnt_t tw_refcount; /* (Space lock) */
704 task_t tw_task; /* (Space lock) & tw_refcount == 0 */
705 thread_t tw_thread; /* (Space lock) & tw_refcount == 0 */
706 uint32_t tw_elem_array_count; /* (Space lock) */
707 struct task_watchport_elem tw_elem[]; /* (Space lock) & (Portlock) & (mq lock) */
708 };
709
710 #define task_watchports_retain(x) (os_ref_retain(&(x)->tw_refcount))
711 #define task_watchports_release(x) (os_ref_release(&(x)->tw_refcount))
712
713 #define task_watchport_elem_init(elem, task, port) \
714 do { \
715 (elem)->twe_task = (task); \
716 (elem)->twe_port = (port); \
717 (elem)->twe_pdrequest = IP_NULL; \
718 } while(0)
719
720 #define task_watchport_elem_clear(elem) task_watchport_elem_init((elem), NULL, NULL)
721
722 extern void
723 task_add_turnstile_watchports(
724 task_t task,
725 thread_t thread,
726 ipc_port_t *portwatch_ports,
727 uint32_t portwatch_count);
728
729 extern void
730 task_watchport_elem_deallocate(
731 struct task_watchport_elem *watchport_elem);
732
733 extern boolean_t
734 task_has_watchports(task_t task);
735
736 void
737 task_dyld_process_info_update_helper(
738 task_t task,
739 size_t active_count,
740 vm_map_address_t magic_addr,
741 ipc_port_t *release_ports,
742 size_t release_count);
743
744 extern kern_return_t
745 task_suspend2_mig(
746 task_t task,
747 task_suspension_token_t *suspend_token);
748
749 extern kern_return_t
750 task_suspend2_external(
751 task_t task,
752 task_suspension_token_t *suspend_token);
753
754 extern kern_return_t
755 task_resume2_mig(
756 task_suspension_token_t suspend_token);
757
758 extern kern_return_t
759 task_resume2_external(
760 task_suspension_token_t suspend_token);
761
762 extern void
763 task_suspension_token_deallocate_grp(
764 task_suspension_token_t suspend_token,
765 task_grp_t grp);
766
767 extern ipc_port_t
768 convert_task_to_port_with_flavor(
769 task_t task,
770 mach_task_flavor_t flavor,
771 task_grp_t grp);
772
773 extern task_t current_task_early(void) __pure2;
774
775 #else /* MACH_KERNEL_PRIVATE */
776
777 __BEGIN_DECLS
778
779 extern task_t current_task(void) __pure2;
780
781 extern bool task_is_driver(task_t task);
782
783 #define TF_NONE 0
784
785 #define TWF_NONE 0
786 #define TRW_LRETURNWAIT 0x01 /* task is waiting for fork/posix_spawn/exec to complete */
787 #define TRW_LRETURNWAITER 0x02 /* task is waiting for TRW_LRETURNWAIT to get cleared */
788 #define TRW_LEXEC_COMPLETE 0x04 /* thread should call exec complete */
789
790 /* task clear return wait flags */
791 #define TCRW_CLEAR_INITIAL_WAIT 0x1
792 #define TCRW_CLEAR_FINAL_WAIT 0x2
793 #define TCRW_CLEAR_EXEC_COMPLETE 0x4
794 #define TCRW_CLEAR_ALL_WAIT (TCRW_CLEAR_INITIAL_WAIT | TCRW_CLEAR_FINAL_WAIT)
795
796
797 #define TPF_NONE 0
798 #define TPF_EXEC_COPY 0x00000002 /* task is the new copy of an exec */
799
800
801 __END_DECLS
802
803 #endif /* MACH_KERNEL_PRIVATE */
804
805 __BEGIN_DECLS
806
807 #ifdef KERNEL_PRIVATE
808 extern boolean_t task_is_app_suspended(task_t task);
809 extern bool task_is_exotic(task_t task);
810 extern bool task_is_alien(task_t task);
811 extern boolean_t task_get_platform_binary(task_t task);
812 #endif /* KERNEL_PRIVATE */
813
814 #ifdef XNU_KERNEL_PRIVATE
815
816 /* Hold all threads in a task, Wait for task to stop running, just to get off CPU */
817 extern kern_return_t task_hold_and_wait(
818 task_t task,
819 bool suspend_conclave);
820
821 /* Release hold on all threads in a task */
822 extern kern_return_t task_release(
823 task_t task);
824
825 /* Suspend/resume a task where the kernel owns the suspend count */
826 extern kern_return_t task_suspend_internal( task_t task);
827 extern kern_return_t task_resume_internal( task_t task);
828
829 /* Suspends a task by placing a hold on its threads */
830 extern kern_return_t task_pidsuspend(
831 task_t task);
832
833 /* Resumes a previously paused task */
834 extern kern_return_t task_pidresume(
835 task_t task);
836
837 extern kern_return_t task_send_trace_memory(
838 task_t task,
839 uint32_t pid,
840 uint64_t uniqueid);
841
842 extern void task_remove_turnstile_watchports(
843 task_t task);
844
845 extern void task_transfer_turnstile_watchports(
846 task_t old_task,
847 task_t new_task,
848 thread_t new_thread);
849
850 extern kern_return_t
851 task_violated_guard(mach_exception_code_t, mach_exception_subcode_t, void *, bool);
852
853 #if DEVELOPMENT || DEBUG
854
855 extern kern_return_t task_disconnect_page_mappings(
856 task_t task);
857 #endif /* DEVELOPMENT || DEBUG */
858
859 extern void tasks_system_suspend(boolean_t suspend);
860
861 #if CONFIG_FREEZE
862
863 /* Freeze a task's resident pages */
864 extern kern_return_t task_freeze(
865 task_t task,
866 uint32_t *purgeable_count,
867 uint32_t *wired_count,
868 uint32_t *clean_count,
869 uint32_t *dirty_count,
870 uint32_t dirty_budget,
871 uint32_t *shared_count,
872 int *freezer_error_code,
873 boolean_t eval_only);
874
875 /* Thaw a currently frozen task */
876 extern kern_return_t task_thaw(
877 task_t task);
878
879 typedef enum {
880 CREDIT_TO_SWAP = 1,
881 DEBIT_FROM_SWAP = 2
882 } freezer_acct_op_t;
883
884 extern void task_update_frozen_to_swap_acct(
885 task_t task,
886 int64_t amount,
887 freezer_acct_op_t op);
888
889 #endif /* CONFIG_FREEZE */
890
891 /* Halt all other threads in the current task */
892 extern kern_return_t task_start_halt(
893 task_t task);
894
895 /* Wait for other threads to halt and free halting task resources */
896 extern void task_complete_halt(
897 task_t task);
898
899 extern kern_return_t task_terminate_internal(
900 task_t task);
901
902 struct proc_ro;
903 typedef struct proc_ro *proc_ro_t;
904
905 extern kern_return_t task_create_internal(
906 task_t parent_task,
907 proc_ro_t proc_ro,
908 coalition_t *parent_coalitions,
909 boolean_t inherit_memory,
910 boolean_t is_64bit,
911 boolean_t is_64bit_data,
912 uint32_t t_flags,
913 uint32_t t_flags_ro,
914 uint32_t procflags,
915 uint8_t t_returnwaitflags,
916 task_t child_task);
917
918 extern kern_return_t task_set_special_port_internal(
919 task_t task,
920 int which,
921 ipc_port_t port);
922
923 extern kern_return_t task_set_security_tokens(
924 task_t task,
925 security_token_t sec_token,
926 audit_token_t audit_token,
927 host_priv_t host_priv);
928
929 extern kern_return_t task_info(
930 task_t task,
931 task_flavor_t flavor,
932 task_info_t task_info_out,
933 mach_msg_type_number_t *task_info_count);
934
935 /*
936 * Additional fields that aren't exposed through `task_power_info` but needed
937 * by clients of `task_power_info_locked`.
938 */
939 struct task_power_info_extra {
940 uint64_t cycles;
941 uint64_t instructions;
942 uint64_t pcycles;
943 uint64_t pinstructions;
944 uint64_t user_ptime;
945 uint64_t system_ptime;
946 uint64_t runnable_time;
947 uint64_t energy;
948 uint64_t penergy;
949 uint64_t secure_time;
950 uint64_t secure_ptime;
951 };
952
953 void task_power_info_locked(
954 task_t task,
955 task_power_info_t info,
956 gpu_energy_data_t gpu_energy,
957 task_power_info_v2_t infov2,
958 struct task_power_info_extra *extra_info);
959
960 extern uint64_t task_gpu_utilisation(
961 task_t task);
962
963 extern void task_update_cpu_time_qos_stats(
964 task_t task,
965 uint64_t *eqos_stats,
966 uint64_t *rqos_stats);
967
968 extern void task_vtimer_set(
969 task_t task,
970 integer_t which);
971
972 extern void task_vtimer_clear(
973 task_t task,
974 integer_t which);
975
976 extern void task_vtimer_update(
977 task_t task,
978 integer_t which,
979 uint32_t *microsecs);
980
981 #define TASK_VTIMER_USER 0x01
982 #define TASK_VTIMER_PROF 0x02
983 #define TASK_VTIMER_RLIM 0x04
984
985 extern void task_set_64bit(
986 task_t task,
987 boolean_t is_64bit,
988 boolean_t is_64bit_data);
989
990 extern bool task_get_64bit_addr(
991 task_t task);
992
993 extern bool task_get_64bit_data(
994 task_t task);
995
996 extern void task_set_platform_binary(
997 task_t task,
998 boolean_t is_platform);
999
1000 #if XNU_TARGET_OS_OSX
1001 #if DEVELOPMENT || DEBUG
1002 /* Disables task identity security hardening (*_set_exception_ports policy)
1003 * for all tasks if amfi_get_out_of_my_way is set. */
1004 extern bool AMFI_bootarg_disable_mach_hardening;
1005 #endif /* DEVELOPMENT || DEBUG */
1006 extern void task_disable_mach_hardening(
1007 task_t task);
1008
1009 extern bool task_opted_out_mach_hardening(
1010 task_t task);
1011 #endif /* XNU_TARGET_OS_OSX */
1012
1013 extern void
1014 task_set_hardened_runtime(
1015 task_t task,
1016 bool is_hardened);
1017
1018 extern boolean_t
1019 task_is_hardened_binary(
1020 task_t task);
1021
1022 extern boolean_t task_is_a_corpse(
1023 task_t task);
1024
1025 extern boolean_t task_is_ipc_active(
1026 task_t task);
1027
1028 extern void task_set_corpse(
1029 task_t task);
1030
1031 extern void task_set_exc_guard_ctrl_port_default(
1032 task_t task,
1033 thread_t main_thread,
1034 const char *name,
1035 unsigned int namelen,
1036 boolean_t is_simulated,
1037 uint32_t platform,
1038 uint32_t sdk);
1039
1040 extern void task_set_immovable_pinned(task_t task);
1041
1042 extern bool task_set_ca_client_wi(
1043 task_t task,
1044 boolean_t ca_client_wi);
1045
1046 extern kern_return_t task_set_dyld_info(
1047 task_t task,
1048 mach_vm_address_t addr,
1049 mach_vm_size_t size,
1050 bool finalize_value);
1051
1052 extern void task_set_mach_header_address(
1053 task_t task,
1054 mach_vm_address_t addr);
1055
1056 extern void task_set_uniqueid(task_t task);
1057
1058 /* Get number of activations in a task */
1059 extern int get_task_numacts(
1060 task_t task);
1061
1062 extern bool task_donates_own_pages(
1063 task_t task);
1064
1065 struct label;
1066 extern kern_return_t task_collect_crash_info(
1067 task_t task,
1068 #if CONFIG_MACF
1069 struct label *crash_label,
1070 #endif
1071 int is_corpse_fork);
1072 void task_wait_till_threads_terminate_locked(task_t task);
1073
1074 /* JMM - should just be temporary (implementation in bsd_kern still) */
1075 extern void set_bsdtask_info(task_t, void *);
1076 extern uint32_t set_task_loadTag(task_t task, uint32_t loadTag);
1077 extern vm_map_t get_task_map_reference(task_t);
1078 extern vm_map_t swap_task_map(task_t, thread_t, vm_map_t);
1079 extern pmap_t get_task_pmap(task_t);
1080 extern uint64_t get_task_resident_size(task_t);
1081 extern uint64_t get_task_compressed(task_t);
1082 extern uint64_t get_task_resident_max(task_t);
1083 extern uint64_t get_task_phys_footprint(task_t);
1084 #if CONFIG_LEDGER_INTERVAL_MAX
1085 extern uint64_t get_task_phys_footprint_interval_max(task_t, int reset);
1086 #endif /* CONFIG_FOOTPRINT_INTERVAL_MAX */
1087 extern uint64_t get_task_phys_footprint_lifetime_max(task_t);
1088 extern uint64_t get_task_phys_footprint_limit(task_t);
1089 extern uint64_t get_task_neural_nofootprint_total(task_t task);
1090 #if CONFIG_LEDGER_INTERVAL_MAX
1091 extern uint64_t get_task_neural_nofootprint_total_interval_max(task_t, int reset);
1092 #endif /* CONFIG_NEURAL_INTERVAL_MAX */
1093 extern uint64_t get_task_neural_nofootprint_total_lifetime_max(task_t);
1094 extern uint64_t get_task_purgeable_size(task_t);
1095 extern uint64_t get_task_cpu_time(task_t);
1096 extern uint64_t get_task_dispatchqueue_offset(task_t);
1097 extern uint64_t get_task_dispatchqueue_serialno_offset(task_t);
1098 extern uint64_t get_task_dispatchqueue_label_offset(task_t);
1099 extern uint64_t get_task_uniqueid(task_t task);
1100 extern int get_task_version(task_t task);
1101
1102 extern uint64_t get_task_internal(task_t);
1103 extern uint64_t get_task_internal_compressed(task_t);
1104 extern uint64_t get_task_purgeable_nonvolatile(task_t);
1105 extern uint64_t get_task_purgeable_nonvolatile_compressed(task_t);
1106 extern uint64_t get_task_iokit_mapped(task_t);
1107 extern uint64_t get_task_alternate_accounting(task_t);
1108 extern uint64_t get_task_alternate_accounting_compressed(task_t);
1109 extern uint64_t get_task_memory_region_count(task_t);
1110 extern uint64_t get_task_page_table(task_t);
1111 #if CONFIG_FREEZE
1112 extern uint64_t get_task_frozen_to_swap(task_t);
1113 #endif
1114 extern uint64_t get_task_network_nonvolatile(task_t);
1115 extern uint64_t get_task_network_nonvolatile_compressed(task_t);
1116 extern uint64_t get_task_wired_mem(task_t);
1117 extern uint32_t get_task_loadTag(task_t task);
1118
1119 extern uint64_t get_task_tagged_footprint(task_t task);
1120 extern uint64_t get_task_tagged_footprint_compressed(task_t task);
1121 extern uint64_t get_task_media_footprint(task_t task);
1122 extern uint64_t get_task_media_footprint_compressed(task_t task);
1123 extern uint64_t get_task_graphics_footprint(task_t task);
1124 extern uint64_t get_task_graphics_footprint_compressed(task_t task);
1125 extern uint64_t get_task_neural_footprint(task_t task);
1126 extern uint64_t get_task_neural_footprint_compressed(task_t task);
1127
1128 extern kern_return_t task_convert_phys_footprint_limit(int, int *);
1129 extern kern_return_t task_set_phys_footprint_limit_internal(task_t, int, int *, boolean_t, boolean_t);
1130 extern kern_return_t task_get_phys_footprint_limit(task_t task, int *limit_mb);
1131 #if DEBUG || DEVELOPMENT
1132 #if CONFIG_MEMORYSTATUS
1133 extern kern_return_t task_set_diag_footprint_limit_internal(task_t, uint64_t, uint64_t *);
1134 extern kern_return_t task_get_diag_footprint_limit_internal(task_t, uint64_t *, bool *);
1135 extern kern_return_t task_set_diag_footprint_limit(task_t task, uint64_t new_limit_mb, uint64_t *old_limit_mb);
1136 #endif /* CONFIG_MEMORYSTATUS */
1137 #endif /* DEBUG || DEVELOPMENT */
1138
1139 extern security_token_t *task_get_sec_token(task_t task);
1140 extern void task_set_sec_token(task_t task, security_token_t *token);
1141 extern audit_token_t *task_get_audit_token(task_t task);
1142 extern void task_set_audit_token(task_t task, audit_token_t *token);
1143 extern void task_set_tokens(task_t task, security_token_t *sec_token, audit_token_t *audit_token);
1144 extern boolean_t task_is_privileged(task_t task);
1145 extern uint8_t *task_get_mach_trap_filter_mask(task_t task);
1146 extern void task_set_mach_trap_filter_mask(task_t task, uint8_t *mask);
1147 extern uint8_t *task_get_mach_kobj_filter_mask(task_t task);
1148 extern void task_set_mach_kobj_filter_mask(task_t task, uint8_t *mask);
1149 extern mach_vm_address_t task_get_all_image_info_addr(task_t task);
1150
1151 /* Jetsam memlimit attributes */
1152 extern boolean_t task_get_memlimit_is_active(task_t task);
1153 extern boolean_t task_get_memlimit_is_fatal(task_t task);
1154 extern void task_set_memlimit_is_active(task_t task, boolean_t memlimit_is_active);
1155 extern void task_set_memlimit_is_fatal(task_t task, boolean_t memlimit_is_fatal);
1156 extern boolean_t task_has_triggered_exc_resource(task_t task, boolean_t memlimit_is_active);
1157 extern void task_mark_has_triggered_exc_resource(task_t task, boolean_t memlimit_is_active);
1158
1159 extern uint64_t task_get_dirty_start(task_t task);
1160 extern void task_set_dirty_start(task_t task, uint64_t start);
1161
1162 extern void task_set_thread_limit(task_t task, uint16_t thread_limit);
1163 #if CONFIG_PROC_RESOURCE_LIMITS
1164 extern kern_return_t task_set_port_space_limits(task_t task, uint32_t soft_limit, uint32_t hard_limit);
1165 #endif /* CONFIG_PROC_RESOURCE_LIMITS */
1166 extern void task_port_space_ast(task_t task);
1167
1168 #if XNU_TARGET_OS_OSX
1169 extern boolean_t task_has_system_version_compat_enabled(task_t task);
1170 extern void task_set_system_version_compat_enabled(task_t task, boolean_t enable_system_version_compat);
1171 #endif
1172
1173 extern boolean_t is_kerneltask(task_t task);
1174 extern boolean_t is_corpsefork(task_t task);
1175
1176 extern kern_return_t check_actforsig(task_t task, thread_t thread, int setast);
1177
1178 extern kern_return_t machine_task_get_state(
1179 task_t task,
1180 int flavor,
1181 thread_state_t state,
1182 mach_msg_type_number_t *state_count);
1183
1184 extern kern_return_t machine_task_set_state(
1185 task_t task,
1186 int flavor,
1187 thread_state_t state,
1188 mach_msg_type_number_t state_count);
1189
1190 extern void machine_task_terminate(task_t task);
1191
1192 extern kern_return_t machine_task_process_signature(task_t task, uint32_t platform, uint32_t sdk, char const **error_msg);
1193
1194 struct _task_ledger_indices {
1195 int cpu_time;
1196 int tkm_private;
1197 int tkm_shared;
1198 int phys_mem;
1199 int wired_mem;
1200 int conclave_mem;
1201 int internal;
1202 int iokit_mapped;
1203 int external;
1204 int reusable;
1205 int alternate_accounting;
1206 int alternate_accounting_compressed;
1207 int page_table;
1208 int phys_footprint;
1209 int internal_compressed;
1210 int purgeable_volatile;
1211 int purgeable_nonvolatile;
1212 int purgeable_volatile_compressed;
1213 int purgeable_nonvolatile_compressed;
1214 int tagged_nofootprint;
1215 int tagged_footprint;
1216 int tagged_nofootprint_compressed;
1217 int tagged_footprint_compressed;
1218 int network_volatile;
1219 int network_nonvolatile;
1220 int network_volatile_compressed;
1221 int network_nonvolatile_compressed;
1222 int media_nofootprint;
1223 int media_footprint;
1224 int media_nofootprint_compressed;
1225 int media_footprint_compressed;
1226 int graphics_nofootprint;
1227 int graphics_footprint;
1228 int graphics_nofootprint_compressed;
1229 int graphics_footprint_compressed;
1230 int neural_nofootprint;
1231 int neural_footprint;
1232 int neural_nofootprint_compressed;
1233 int neural_footprint_compressed;
1234 int neural_nofootprint_total;
1235 int platform_idle_wakeups;
1236 int interrupt_wakeups;
1237 #if CONFIG_SCHED_SFI
1238 int sfi_wait_times[MAX_SFI_CLASS_ID];
1239 #endif /* CONFIG_SCHED_SFI */
1240 int cpu_time_billed_to_me;
1241 int cpu_time_billed_to_others;
1242 int physical_writes;
1243 int logical_writes;
1244 int logical_writes_to_external;
1245 int energy_billed_to_me;
1246 int energy_billed_to_others;
1247 #if CONFIG_MEMORYSTATUS
1248 int memorystatus_dirty_time;
1249 #endif /* CONFIG_MEMORYSTATUS */
1250 int pages_grabbed;
1251 int pages_grabbed_kern;
1252 int pages_grabbed_iopl;
1253 int pages_grabbed_upl;
1254 #if CONFIG_FREEZE
1255 int frozen_to_swap;
1256 #endif /* CONFIG_FREEZE */
1257 #if CONFIG_PHYS_WRITE_ACCT
1258 int fs_metadata_writes;
1259 #endif /* CONFIG_PHYS_WRITE_ACCT */
1260 int swapins;
1261 };
1262
1263 /*
1264 * Each runtime security mitigation that we support for userland processes
1265 * is tracked in the task security configuration and managed by the following
1266 * helpers.
1267 */
1268 #define TASK_SECURITY_CONFIG_HELPER_DECLARE(suffix) \
1269 extern bool task_has_##suffix(task_t); \
1270 extern void task_set_##suffix(task_t); \
1271 extern void task_clear_##suffix(task_t)
1272
1273 extern uint32_t task_get_security_config(task_t);
1274
1275 TASK_SECURITY_CONFIG_HELPER_DECLARE(hardened_heap);
1276 TASK_SECURITY_CONFIG_HELPER_DECLARE(tpro);
1277
1278
1279 /*
1280 * Many of the task ledger entries use a reduced feature set
1281 * (specifically they just use LEDGER_ENTRY_ALLOW_PANIC_ON_NEGATIVE)
1282 * and are stored in a smaller entry structure.
1283 * That structure is an implementation detail of the ledger.
1284 * But on PPL systems, the task ledger's memory is managed by the PPL
1285 * and it has to determine the size of the task ledger at compile time.
1286 * This define specifies the number of small entries so the PPL can
1287 * properly determine the ledger's size.
1288 *
1289 * If you add a new entry with only the
1290 * LEDGER_ENTRY_ALLOW_PANIC_ON_NEGATIVE | LEDGER_ENTRY_ALLOW_INACTIVE
1291 * flags, you need to increment this count.
1292 * Otherwise, PPL systems will panic at boot.
1293 */
1294 #define TASK_LEDGER_NUM_SMALL_INDICES 33
1295 extern struct _task_ledger_indices task_ledgers;
1296
1297 /* requires task to be unlocked, returns a referenced thread */
1298 thread_t task_findtid(task_t task, uint64_t tid);
1299 int pid_from_task(task_t task);
1300
1301 extern kern_return_t task_wakeups_monitor_ctl(task_t task, uint32_t *rate_hz, int32_t *flags);
1302 extern kern_return_t task_cpu_usage_monitor_ctl(task_t task, uint32_t *flags);
1303 extern void task_rollup_accounting_info(task_t new_task, task_t parent_task);
1304 extern kern_return_t task_io_monitor_ctl(task_t task, uint32_t *flags);
1305 extern void task_set_did_exec_flag(task_t task);
1306 extern void task_clear_exec_copy_flag(task_t task);
1307 extern boolean_t task_is_exec_copy(task_t);
1308 extern boolean_t task_did_exec(task_t task);
1309 extern boolean_t task_is_active(task_t task);
1310 extern boolean_t task_is_halting(task_t task);
1311 extern void task_clear_return_wait(task_t task, uint32_t flags);
1312 extern void task_wait_to_return(void) __attribute__((noreturn));
1313 extern void task_post_signature_processing_hook(task_t task);
1314 extern event_t task_get_return_wait_event(task_t task);
1315
1316 extern void task_bank_reset(task_t task);
1317 extern void task_bank_init(task_t task);
1318
1319 #if CONFIG_MEMORYSTATUS
1320 extern void task_ledger_settle_dirty_time(task_t t);
1321 extern void task_ledger_settle_dirty_time_locked(task_t t);
1322 #endif /* CONFIG_MEMORYSTATUS */
1323 extern void task_ledger_settle(task_t t);
1324
1325 #if CONFIG_ARCADE
1326 extern void task_prep_arcade(task_t task, thread_t thread);
1327 #endif /* CONFIG_ARCADE */
1328
1329 extern int task_pid(task_t task);
1330
1331 #if __has_feature(ptrauth_calls)
1332 char *task_get_vm_shared_region_id_and_jop_pid(task_t task, uint64_t *);
1333 void task_set_shared_region_id(task_t task, char *id);
1334 #endif /* __has_feature(ptrauth_calls) */
1335
1336 extern boolean_t task_has_assertions(task_t task);
1337 /* End task_policy */
1338
1339 extern void task_set_gpu_denied(task_t task, boolean_t denied);
1340 extern boolean_t task_is_gpu_denied(task_t task);
1341
1342 extern void task_set_game_mode(task_t task, bool enabled);
1343 /* returns true if update must be pushed to coalition (Automatically handled by task_set_game_mode) */
1344 extern bool task_set_game_mode_locked(task_t task, bool enabled);
1345 extern bool task_get_game_mode(task_t task);
1346
1347 extern void task_set_carplay_mode(task_t task, bool enabled);
1348 /* returns true if update must be pushed to coalition (Automatically handled by task_set_carplay_mode) */
1349 extern bool task_set_carplay_mode_locked(task_t task, bool enabled);
1350 extern bool task_get_carplay_mode(task_t task);
1351
1352 extern queue_head_t * task_io_user_clients(task_t task);
1353 extern void task_set_message_app_suspended(task_t task, boolean_t enable);
1354
1355 extern void task_copy_fields_for_exec(task_t dst_task, task_t src_task);
1356
1357 extern void task_copy_vmobjects(task_t task, vm_object_query_t query, size_t len, size_t *num);
1358 extern void task_get_owned_vmobjects(task_t task, size_t buffer_size, vmobject_list_output_t buffer, size_t* output_size, size_t* entries);
1359
1360 extern void task_set_filter_msg_flag(task_t task, boolean_t flag);
1361 extern boolean_t task_get_filter_msg_flag(task_t task);
1362
1363 #if __has_feature(ptrauth_calls)
1364 extern bool task_is_pac_exception_fatal(task_t task);
1365 extern void task_set_pac_exception_fatal_flag(task_t task);
1366 #endif /*__has_feature(ptrauth_calls)*/
1367
1368 extern bool task_is_jit_exception_fatal(task_t task);
1369 extern void task_set_jit_flags(task_t task);
1370
1371 extern bool task_needs_user_signed_thread_state(task_t task);
1372 extern void task_set_tecs(task_t task);
1373 extern void task_get_corpse_vmobject_list(task_t task, vmobject_list_output_t* list, size_t* list_size);
1374
1375 extern boolean_t task_corpse_forking_disabled(task_t task);
1376
1377 void __attribute__((noinline)) SENDING_NOTIFICATION__THIS_PROCESS_HAS_TOO_MANY_MACH_PORTS(task_t task,
1378 uint32_t current_size, uint32_t soft_limit, uint32_t hard_limit);
1379
1380 extern int get_task_cdhash(task_t task, char cdhash[CS_CDHASH_LEN]);
1381
1382 extern boolean_t kdp_task_is_locked(task_t task);
1383
1384 /* redeclaration from task_server.h for the sake of kern_exec.c */
1385 extern kern_return_t _kernelrpc_mach_ports_register3(
1386 task_t task,
1387 mach_port_t port1,
1388 mach_port_t port2,
1389 mach_port_t port3);
1390
1391 /* Kernel side prototypes for MIG routines */
1392 extern kern_return_t task_get_exception_ports(
1393 task_t task,
1394 exception_mask_t exception_mask,
1395 exception_mask_array_t masks,
1396 mach_msg_type_number_t *CountCnt,
1397 exception_port_array_t ports,
1398 exception_behavior_array_t behaviors,
1399 thread_state_flavor_array_t flavors);
1400
1401 #if CONFIG_EXCLAVES
1402 int task_add_conclave(task_t task, void *, int64_t, const char *task_conclave_id);
1403 kern_return_t task_inherit_conclave(task_t old_task, task_t new_task, void *vnode, int64_t off);
1404 kern_return_t task_launch_conclave(mach_port_name_t port);
1405 void task_clear_conclave(task_t task);
1406 void task_stop_conclave(task_t task, bool gather_crash_bt);
1407 void task_suspend_conclave(task_t task);
1408 void task_resume_conclave(task_t task);
1409 kern_return_t task_stop_conclave_upcall(void);
1410 kern_return_t task_stop_conclave_upcall_complete(void);
1411 kern_return_t task_suspend_conclave_upcall(uint64_t *, size_t);
1412 struct conclave_sharedbuffer_t;
1413 kern_return_t task_crash_info_conclave_upcall(task_t task,
1414 const struct conclave_sharedbuffer_t *shared_buf, uint32_t length);
1415 typedef struct exclaves_resource exclaves_resource_t;
1416 exclaves_resource_t *task_get_conclave(task_t task);
1417 void task_set_conclave_untaintable(task_t task);
1418 void task_add_conclave_crash_info(task_t task, void *crash_info_ptr);
1419 //Changing this would also warrant a change in ConclaveSharedBuffer
1420 #define CONCLAVE_CRASH_BUFFER_PAGECOUNT 2
1421
1422 #endif /* CONFIG_EXCLAVES */
1423
1424 #endif /* XNU_KERNEL_PRIVATE */
1425 #ifdef KERNEL_PRIVATE
1426
1427 extern void *get_bsdtask_info(task_t);
1428 extern void *get_bsdthreadtask_info(thread_t);
1429 extern void task_bsdtask_kill(task_t);
1430 extern vm_map_t get_task_map(task_t);
1431 extern ledger_t get_task_ledger(task_t);
1432
1433 extern boolean_t get_task_pidsuspended(task_t);
1434 extern boolean_t get_task_suspended(task_t);
1435 extern boolean_t get_task_frozen(task_t);
1436
1437 /*
1438 * Flavors of convert_task_to_port. XNU callers get convert_task_to_port_kernel,
1439 * external callers get convert_task_to_port_external.
1440 */
1441 extern ipc_port_t convert_task_to_port(task_t);
1442 extern ipc_port_t convert_task_to_port_kernel(task_t);
1443 extern ipc_port_t convert_task_to_port_external(task_t);
1444 extern ipc_port_t convert_task_to_port_pinned(task_t);
1445 extern void convert_task_array_to_ports(task_array_t, size_t, mach_task_flavor_t);
1446
1447 extern ipc_port_t convert_task_read_to_port(task_t);
1448 extern ipc_port_t convert_task_read_to_port_kernel(task_read_t);
1449 extern ipc_port_t convert_task_read_to_port_external(task_t);
1450
1451 extern ipc_port_t convert_task_inspect_to_port(task_inspect_t);
1452 extern ipc_port_t convert_task_name_to_port(task_name_t);
1453
1454 extern ipc_port_t convert_corpse_to_port_and_nsrequest(task_t task);
1455
1456 extern ipc_port_t convert_task_suspension_token_to_port(task_suspension_token_t task);
1457 /* Convert from a port (in this case, an SO right to a task's resume port) to a task. */
1458 extern task_suspension_token_t convert_port_to_task_suspension_token(ipc_port_t port);
1459
1460 extern void task_suspension_send_once(ipc_port_t port);
1461
1462 #define TASK_WRITE_IMMEDIATE 0x1
1463 #define TASK_WRITE_DEFERRED 0x2
1464 #define TASK_WRITE_INVALIDATED 0x4
1465 #define TASK_WRITE_METADATA 0x8
1466 extern void task_update_logical_writes(task_t task, uint32_t io_size, int flags, void *vp);
1467
1468 __enum_decl(task_balance_flags_t, uint8_t, {
1469 TASK_BALANCE_CREDIT = 0x1,
1470 TASK_BALANCE_DEBIT = 0x2,
1471 });
1472
1473 __enum_decl(task_physical_write_flavor_t, uint8_t, {
1474 TASK_PHYSICAL_WRITE_METADATA = 0x1,
1475 });
1476 extern void task_update_physical_writes(task_t task, task_physical_write_flavor_t flavor,
1477 uint64_t io_size, task_balance_flags_t flags);
1478
1479 #if CONFIG_SECLUDED_MEMORY
1480 extern void task_set_can_use_secluded_mem(
1481 task_t task,
1482 boolean_t can_use_secluded_mem);
1483 extern void task_set_could_use_secluded_mem(
1484 task_t task,
1485 boolean_t could_use_secluded_mem);
1486 extern void task_set_could_also_use_secluded_mem(
1487 task_t task,
1488 boolean_t could_also_use_secluded_mem);
1489 extern boolean_t task_can_use_secluded_mem(
1490 task_t task,
1491 boolean_t is_allocate);
1492 extern boolean_t task_could_use_secluded_mem(task_t task);
1493 extern boolean_t task_could_also_use_secluded_mem(task_t task);
1494 #endif /* CONFIG_SECLUDED_MEMORY */
1495
1496 extern void task_set_darkwake_mode(task_t, boolean_t);
1497 extern boolean_t task_get_darkwake_mode(task_t);
1498
1499 #if __arm64__
1500 extern void task_set_legacy_footprint(task_t task);
1501 extern void task_set_extra_footprint_limit(task_t task);
1502 extern void task_set_ios13extended_footprint_limit(task_t task);
1503 #endif /* __arm64__ */
1504
1505 #if CONFIG_MACF
1506 extern struct label *get_task_crash_label(task_t task);
1507 extern void set_task_crash_label(task_t task, struct label *label);
1508 #endif /* CONFIG_MACF */
1509
1510 /* task_find_region_details() */
1511 __options_closed_decl(find_region_details_options_t, uint32_t, {
1512 FIND_REGION_DETAILS_OPTIONS_NONE = 0x00000000,
1513 FIND_REGION_DETAILS_AT_OFFSET = 0x00000001,
1514 FIND_REGION_DETAILS_GET_VNODE = 0x00000002,
1515 });
1516 #define FIND_REGION_DETAILS_OPTIONS_ALL ( \
1517 FIND_REGION_DETAILS_AT_OFFSET | \
1518 FIND_REGION_DETAILS_GET_VNODE \
1519 )
1520 extern int task_find_region_details(
1521 task_t task,
1522 vm_map_offset_t offset,
1523 find_region_details_options_t options,
1524 uintptr_t *vp_p, /* caller must call vnode_put(vp) when done */
1525 uint32_t *vid_p,
1526 bool *is_mapped_shared_p,
1527 uint64_t *start_p,
1528 uint64_t *len_p);
1529
1530
1531 #endif /* KERNEL_PRIVATE */
1532
1533 extern task_t kernel_task;
1534
1535 extern void task_name_deallocate_mig(
1536 task_name_t task_name);
1537
1538 extern void task_policy_set_deallocate_mig(
1539 task_policy_set_t task_policy_set);
1540
1541 extern void task_policy_get_deallocate_mig(
1542 task_policy_get_t task_policy_get);
1543
1544 extern void task_inspect_deallocate_mig(
1545 task_inspect_t task_inspect);
1546
1547 extern void task_read_deallocate_mig(
1548 task_read_t task_read);
1549
1550 extern void task_suspension_token_deallocate(
1551 task_suspension_token_t token);
1552
1553 extern boolean_t task_self_region_footprint(void);
1554 extern void task_self_region_footprint_set(boolean_t newval);
1555
1556 /* VM_REGION_INFO_FLAGS defined in vm_region.h */
1557 extern int task_self_region_info_flags(void);
1558 extern kern_return_t task_self_region_info_flags_set(int newval);
1559
1560 extern void task_ledgers_footprint(ledger_t ledger,
1561 ledger_amount_t *ledger_resident,
1562 ledger_amount_t *ledger_compressed);
1563 extern void task_set_memory_ownership_transfer(
1564 task_t task,
1565 boolean_t value);
1566
1567 #if DEVELOPMENT || DEBUG
1568 extern void task_set_no_footprint_for_debug(
1569 task_t task,
1570 boolean_t value);
1571 extern int task_get_no_footprint_for_debug(
1572 task_t task);
1573 #endif /* DEVELOPMENT || DEBUG */
1574
1575 #ifdef KERNEL_PRIVATE
1576 extern kern_return_t task_get_suspend_stats(task_t task, task_suspend_stats_t stats);
1577 extern kern_return_t task_get_suspend_stats_kdp(task_t task, task_suspend_stats_t stats);
1578 #endif /* KERNEL_PRIVATE*/
1579
1580 #ifdef XNU_KERNEL_PRIVATE
1581 extern kern_return_t task_get_suspend_sources(task_t task, task_suspend_source_array_t sources);
1582 extern kern_return_t task_get_suspend_sources_kdp(task_t task, task_suspend_source_array_t sources);
1583 #endif /* XNU_KERNEL_PRIVATE */
1584
1585 #if CONFIG_ROSETTA
1586 extern bool task_is_translated(task_t task);
1587 #endif
1588
1589
1590 #ifdef MACH_KERNEL_PRIVATE
1591
1592 void task_procname(task_t task, char *buf, int size);
1593 const char *task_best_name(task_t task);
1594
1595 void task_set_ast_mach_exception(task_t task);
1596
1597 #endif /* MACH_KERNEL_PRIVATE */
1598
1599
1600
1601 #ifdef KERNEL_PRIVATE
1602 kern_return_t task_set_cs_auxiliary_info(task_t task, uint64_t info);
1603 uint64_t task_get_cs_auxiliary_info_kdp(task_t task);
1604 #endif /* KERNEL_PRIVATE */
1605
1606 __END_DECLS
1607
1608 #endif /* _KERN_TASK_H_ */
1609