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