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