]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/task.h
xnu-6153.141.1.tar.gz
[apple/xnu.git] / osfmk / kern / task.h
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 Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58 /*
59 * File: 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 csl-dist@cs.utah.edu 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 <mach/mach_types.h>
96 #include <sys/cdefs.h>
97
98 #ifdef XNU_KERNEL_PRIVATE
99 #include <kern/kern_cdata.h>
100 #include <mach/sfi_class.h>
101 #include <kern/queue.h>
102 #include <sys/kern_sysctl.h>
103 #endif /* XNU_KERNEL_PRIVATE */
104
105 #ifdef MACH_KERNEL_PRIVATE
106
107 #include <mach/boolean.h>
108 #include <mach/port.h>
109 #include <mach/time_value.h>
110 #include <mach/message.h>
111 #include <mach/mach_param.h>
112 #include <mach/task_info.h>
113 #include <mach/exception_types.h>
114 #include <mach/vm_statistics.h>
115 #include <machine/task.h>
116
117 #if MONOTONIC
118 #include <machine/monotonic.h>
119 #endif /* MONOTONIC */
120
121 #include <kern/cpu_data.h>
122 #include <kern/queue.h>
123 #include <kern/exception.h>
124 #include <kern/locks.h>
125 #include <security/_label.h>
126 #include <ipc/ipc_port.h>
127
128 #include <kern/thread.h>
129 #include <mach/coalition.h>
130 #include <stdatomic.h>
131 #include <os/refcnt.h>
132
133 #ifdef CONFIG_ATM
134 #include <atm/atm_internal.h>
135 #endif
136
137 struct _cpu_time_qos_stats {
138 uint64_t cpu_time_qos_default;
139 uint64_t cpu_time_qos_maintenance;
140 uint64_t cpu_time_qos_background;
141 uint64_t cpu_time_qos_utility;
142 uint64_t cpu_time_qos_legacy;
143 uint64_t cpu_time_qos_user_initiated;
144 uint64_t cpu_time_qos_user_interactive;
145 };
146
147 struct task_writes_counters {
148 uint64_t task_immediate_writes;
149 uint64_t task_deferred_writes;
150 uint64_t task_invalidated_writes;
151 uint64_t task_metadata_writes;
152 };
153
154 struct task_watchports;
155 #include <bank/bank_internal.h>
156
157 struct task {
158 /* Synchronization/destruction information */
159 decl_lck_mtx_data(, lock); /* Task's lock */
160 os_refcnt_t ref_count; /* Number of references to me */
161 boolean_t active; /* Task has not been terminated */
162 boolean_t halting; /* Task is being halted */
163 boolean_t message_app_suspended; /* Let iokit know when pidsuspended */
164
165 /* Virtual timers */
166 uint32_t vtimers;
167
168 /* Miscellaneous */
169 vm_map_t map; /* Address space description */
170 queue_chain_t tasks; /* global list of tasks */
171 struct task_watchports *watchports; /* watchports passed in spawn */
172 turnstile_inheritor_t returnwait_inheritor; /* inheritor for task_wait */
173
174 #if defined(CONFIG_SCHED_MULTIQ)
175 sched_group_t sched_group;
176 #endif /* defined(CONFIG_SCHED_MULTIQ) */
177
178 /* Threads in this task */
179 queue_head_t threads;
180 struct restartable_ranges *restartable_ranges;
181
182 processor_set_t pset_hint;
183 struct affinity_space *affinity_space;
184
185 int thread_count;
186 uint32_t active_thread_count;
187 int suspend_count; /* Internal scheduling only */
188
189 /* User-visible scheduling information */
190 integer_t user_stop_count; /* outstanding stops */
191 integer_t legacy_stop_count; /* outstanding legacy stops */
192
193 integer_t priority; /* base priority for threads */
194 integer_t max_priority; /* maximum priority for threads */
195
196 integer_t importance; /* priority offset (BSD 'nice' value) */
197
198 /* Task security and audit tokens */
199 security_token_t sec_token;
200 audit_token_t audit_token;
201
202 /* Statistics */
203 uint64_t total_user_time; /* terminated threads only */
204 uint64_t total_system_time;
205 uint64_t total_ptime;
206 uint64_t total_runnable_time;
207
208 /* IPC structures */
209 decl_lck_mtx_data(, itk_lock_data);
210 struct ipc_port *itk_self; /* not a right, doesn't hold ref */
211 struct ipc_port *itk_nself; /* not a right, doesn't hold ref */
212 struct ipc_port *itk_sself; /* a send right */
213 struct exception_action exc_actions[EXC_TYPES_COUNT];
214 /* a send right each valid element */
215 struct ipc_port *itk_host; /* a send right */
216 struct ipc_port *itk_bootstrap; /* a send right */
217 struct ipc_port *itk_seatbelt; /* a send right */
218 struct ipc_port *itk_gssd; /* yet another send right */
219 struct ipc_port *itk_debug_control; /* send right for debugmode communications */
220 struct ipc_port *itk_task_access; /* and another send right */
221 struct ipc_port *itk_resume; /* a receive right to resume this task */
222 struct ipc_port *itk_registered[TASK_PORT_REGISTER_MAX];
223 /* all send rights */
224
225 struct ipc_space *itk_space;
226
227 ledger_t ledger;
228 /* Synchronizer ownership information */
229 queue_head_t semaphore_list; /* list of owned semaphores */
230 int semaphores_owned; /* number of semaphores owned */
231
232 unsigned int priv_flags; /* privilege resource flags */
233 #define VM_BACKING_STORE_PRIV 0x1
234
235 MACHINE_TASK
236
237 integer_t faults; /* faults counter */
238 integer_t decompressions; /* decompression counter */
239 integer_t pageins; /* pageins counter */
240 integer_t cow_faults; /* copy on write fault counter */
241 integer_t messages_sent; /* messages sent counter */
242 integer_t messages_received; /* messages received counter */
243 integer_t syscalls_mach; /* mach system call counter */
244 integer_t syscalls_unix; /* unix system call counter */
245 uint32_t c_switch; /* total context switches */
246 uint32_t p_switch; /* total processor switches */
247 uint32_t ps_switch; /* total pset switches */
248
249 #ifdef MACH_BSD
250 void *bsd_info;
251 #endif
252 kcdata_descriptor_t corpse_info;
253 uint64_t crashed_thread_id;
254 queue_chain_t corpse_tasks;
255 #ifdef CONFIG_MACF
256 struct label * crash_label;
257 #endif
258 struct vm_shared_region *shared_region;
259 volatile uint32_t t_flags; /* general-purpose task flags protected by task_lock (TL) */
260 #define TF_NONE 0
261 #define TF_64B_ADDR 0x00000001 /* task has 64-bit addressing */
262 #define TF_64B_DATA 0x00000002 /* task has 64-bit data registers */
263 #define TF_CPUMON_WARNING 0x00000004 /* task has at least one thread in CPU usage warning zone */
264 #define TF_WAKEMON_WARNING 0x00000008 /* task is in wakeups monitor warning zone */
265 #define TF_TELEMETRY (TF_CPUMON_WARNING | TF_WAKEMON_WARNING) /* task is a telemetry participant */
266 #define TF_GPU_DENIED 0x00000010 /* task is not allowed to access the GPU */
267 #define TF_CORPSE 0x00000020 /* task is a corpse */
268 #define TF_PENDING_CORPSE 0x00000040 /* task corpse has not been reported yet */
269 #define TF_CORPSE_FORK 0x00000080 /* task is a forked corpse */
270 #define TF_PLATFORM 0x00000400 /* task is a platform binary */
271 #define TF_CA_CLIENT_WI 0x00000800 /* task has CA_CLIENT work interval */
272 #define TF_DARKWAKE_MODE 0x00001000 /* task is in darkwake mode */
273 #define TF_NO_SMT 0x00002000 /* task threads must not be paired with SMT threads */
274 #define TF_PAC_EXC_FATAL 0x00004000 /* task is marked a corpse if a PAC exception occurs */
275
276 /*
277 * Task is running within a 64-bit address space.
278 */
279 #define task_has_64Bit_addr(task) \
280 (((task)->t_flags & TF_64B_ADDR) != 0)
281 #define task_set_64Bit_addr(task) \
282 ((task)->t_flags |= TF_64B_ADDR)
283 #define task_clear_64Bit_addr(task) \
284 ((task)->t_flags &= ~TF_64B_ADDR)
285
286 /*
287 * Task is using 64-bit machine state.
288 */
289 #define task_has_64Bit_data(task) \
290 (((task)->t_flags & TF_64B_DATA) != 0)
291 #define task_set_64Bit_data(task) \
292 ((task)->t_flags |= TF_64B_DATA)
293 #define task_clear_64Bit_data(task) \
294 ((task)->t_flags &= ~TF_64B_DATA)
295
296 #define task_is_a_corpse(task) \
297 (((task)->t_flags & TF_CORPSE) != 0)
298
299 #define task_set_corpse(task) \
300 ((task)->t_flags |= TF_CORPSE)
301
302 #define task_corpse_pending_report(task) \
303 (((task)->t_flags & TF_PENDING_CORPSE) != 0)
304
305 #define task_set_corpse_pending_report(task) \
306 ((task)->t_flags |= TF_PENDING_CORPSE)
307
308 #define task_clear_corpse_pending_report(task) \
309 ((task)->t_flags &= ~TF_PENDING_CORPSE)
310
311 #define task_is_a_corpse_fork(task) \
312 (((task)->t_flags & TF_CORPSE_FORK) != 0)
313
314 uint32_t t_procflags; /* general-purpose task flags protected by proc_lock (PL) */
315 #define TPF_NONE 0
316 #define TPF_DID_EXEC 0x00000001 /* task has been execed to a new task */
317 #define TPF_EXEC_COPY 0x00000002 /* task is the new copy of an exec */
318 #ifdef CONFIG_32BIT_TELEMETRY
319 #define TPF_LOG_32BIT_TELEMETRY 0x00000004 /* task should log identifying information */
320 #endif
321
322 #define task_did_exec_internal(task) \
323 (((task)->t_procflags & TPF_DID_EXEC) != 0)
324
325 #define task_is_exec_copy_internal(task) \
326 (((task)->t_procflags & TPF_EXEC_COPY) != 0)
327
328 uint8_t t_returnwaitflags;
329 #define TWF_NONE 0
330 #define TRW_LRETURNWAIT 0x01 /* task is waiting for fork/posix_spawn/exec to complete */
331 #define TRW_LRETURNWAITER 0x02 /* task is waiting for TRW_LRETURNWAIT to get cleared */
332
333 mach_vm_address_t all_image_info_addr; /* dyld __all_image_info */
334 mach_vm_size_t all_image_info_size; /* section location and size */
335
336 #if KPC
337 #define TASK_KPC_FORCED_ALL_CTRS 0x2 /* Bit in "t_kpc" signifying this task forced all counters */
338 uint32_t t_kpc; /* kpc flags */
339 #endif /* KPC */
340
341 boolean_t pidsuspended; /* pid_suspend called; no threads can execute */
342 boolean_t frozen; /* frozen; private resident pages committed to swap */
343 boolean_t changing_freeze_state; /* in the process of freezing or thawing */
344 uint16_t policy_ru_cpu :4,
345 policy_ru_cpu_ext :4,
346 applied_ru_cpu :4,
347 applied_ru_cpu_ext :4;
348 uint8_t rusage_cpu_flags;
349 uint8_t rusage_cpu_percentage; /* Task-wide CPU limit percentage */
350 uint8_t rusage_cpu_perthr_percentage; /* Per-thread CPU limit percentage */
351 #if MACH_ASSERT
352 int8_t suspends_outstanding; /* suspends this task performed in excess of resumes */
353 #endif
354 uint64_t rusage_cpu_interval; /* Task-wide CPU limit interval */
355 uint64_t rusage_cpu_perthr_interval; /* Per-thread CPU limit interval */
356 uint64_t rusage_cpu_deadline;
357 thread_call_t rusage_cpu_callt;
358 #if CONFIG_EMBEDDED
359 queue_head_t task_watchers; /* app state watcher threads */
360 int num_taskwatchers;
361 int watchapplying;
362 #endif /* CONFIG_EMBEDDED */
363
364 #if CONFIG_ATM
365 struct atm_task_descriptor *atm_context; /* pointer to per task atm descriptor */
366 #endif
367 struct bank_task *bank_context; /* pointer to per task bank structure */
368
369 #if IMPORTANCE_INHERITANCE
370 struct ipc_importance_task *task_imp_base; /* Base of IPC importance chain */
371 #endif /* IMPORTANCE_INHERITANCE */
372
373 vm_extmod_statistics_data_t extmod_statistics;
374
375 struct task_requested_policy requested_policy;
376 struct task_effective_policy effective_policy;
377
378 /*
379 * Can be merged with imp_donor bits, once the IMPORTANCE_INHERITANCE macro goes away.
380 */
381 uint32_t low_mem_notified_warn :1, /* warning low memory notification is sent to the task */
382 low_mem_notified_critical :1, /* critical low memory notification is sent to the task */
383 purged_memory_warn :1, /* purgeable memory of the task is purged for warning level pressure */
384 purged_memory_critical :1, /* purgeable memory of the task is purged for critical level pressure */
385 low_mem_privileged_listener :1, /* if set, task would like to know about pressure changes before other tasks on the system */
386 mem_notify_reserved :27; /* reserved for future use */
387
388 uint32_t memlimit_is_active :1, /* if set, use active attributes, otherwise use inactive attributes */
389 memlimit_is_fatal :1, /* if set, exceeding current memlimit will prove fatal to the task */
390 memlimit_active_exc_resource :1, /* if set, suppress exc_resource exception when task exceeds active memory limit */
391 memlimit_inactive_exc_resource :1, /* if set, suppress exc_resource exception when task exceeds inactive memory limit */
392 memlimit_attrs_reserved :28; /* reserved for future use */
393
394 io_stat_info_t task_io_stats;
395
396 struct task_writes_counters task_writes_counters_internal;
397 struct task_writes_counters task_writes_counters_external;
398
399 /*
400 * The cpu_time_qos_stats fields are protected by the task lock
401 */
402 struct _cpu_time_qos_stats cpu_time_eqos_stats;
403 struct _cpu_time_qos_stats cpu_time_rqos_stats;
404
405 /* Statistics accumulated for terminated threads from this task */
406 uint32_t task_timer_wakeups_bin_1;
407 uint32_t task_timer_wakeups_bin_2;
408 uint64_t task_gpu_ns;
409 uint64_t task_energy;
410
411 #if MONOTONIC
412 /* Read and written under task_lock */
413 struct mt_task task_monotonic;
414 #endif /* MONOTONIC */
415
416 uint8_t task_can_transfer_memory_ownership;
417 uint8_t task_objects_disowning;
418 uint8_t task_objects_disowned;
419 /* # of purgeable volatile VM objects owned by this task: */
420 int task_volatile_objects;
421 /* # of purgeable but not volatile VM objects owned by this task: */
422 int task_nonvolatile_objects;
423 int task_owned_objects;
424 queue_head_t task_objq;
425 decl_lck_mtx_data(, task_objq_lock); /* protects "task_objq" */
426
427 unsigned int task_thread_limit:16;
428 #if __arm64__
429 unsigned int task_legacy_footprint:1;
430 unsigned int task_extra_footprint_limit:1;
431 unsigned int task_ios13extended_footprint_limit:1;
432 #endif /* __arm64__ */
433 unsigned int task_region_footprint:1;
434 unsigned int task_has_crossed_thread_limit:1;
435 uint32_t exec_token;
436 /*
437 * A task's coalition set is "adopted" in task_create_internal
438 * and unset in task_deallocate_internal, so each array member
439 * can be referenced without the task lock.
440 * Note: these fields are protected by coalition->lock,
441 * not the task lock.
442 */
443 coalition_t coalition[COALITION_NUM_TYPES];
444 queue_chain_t task_coalition[COALITION_NUM_TYPES];
445 uint64_t dispatchqueue_offset;
446
447 #if DEVELOPMENT || DEBUG
448 boolean_t task_unnested;
449 int task_disconnected_count;
450 #endif
451
452 #if HYPERVISOR
453 void *hv_task_target; /* hypervisor virtual machine object associated with this task */
454 #endif /* HYPERVISOR */
455
456 #if CONFIG_SECLUDED_MEMORY
457 uint8_t task_can_use_secluded_mem;
458 uint8_t task_could_use_secluded_mem;
459 uint8_t task_could_also_use_secluded_mem;
460 uint8_t task_suppressed_secluded;
461 #endif /* CONFIG_SECLUDED_MEMORY */
462
463 task_exc_guard_behavior_t task_exc_guard;
464
465 queue_head_t io_user_clients;
466
467 mach_vm_address_t mach_header_vm_address;
468
469 uint32_t loadTag; /* dext ID used for logging identity */
470 };
471
472 /*
473 * EXC_GUARD default delivery behavior for optional Mach port and VM guards.
474 * Applied to new tasks at creation time.
475 */
476 extern task_exc_guard_behavior_t task_exc_guard_default;
477
478 extern kern_return_t
479 task_violated_guard(mach_exception_code_t, mach_exception_subcode_t, void *);
480
481 #define task_lock(task) lck_mtx_lock(&(task)->lock)
482 #define task_lock_assert_owned(task) LCK_MTX_ASSERT(&(task)->lock, LCK_MTX_ASSERT_OWNED)
483 #define task_lock_try(task) lck_mtx_try_lock(&(task)->lock)
484 #define task_unlock(task) lck_mtx_unlock(&(task)->lock)
485
486 #define task_objq_lock_init(task) lck_mtx_init(&(task)->task_objq_lock, &vm_object_lck_grp, &vm_object_lck_attr)
487 #define task_objq_lock(task) lck_mtx_lock(&(task)->task_objq_lock)
488 #define task_objq_lock_assert_owned(task) LCK_MTX_ASSERT(&(task)->task_objq_lock, LCK_MTX_ASSERT_OWNED)
489 #define task_objq_lock_try(task) lck_mtx_try_lock(&(task)->task_objq_lock)
490 #define task_objq_unlock(task) lck_mtx_unlock(&(task)->task_objq_lock)
491
492 #define itk_lock_init(task) lck_mtx_init(&(task)->itk_lock_data, &ipc_lck_grp, &ipc_lck_attr)
493 #define itk_lock_destroy(task) lck_mtx_destroy(&(task)->itk_lock_data, &ipc_lck_grp)
494 #define itk_lock(task) lck_mtx_lock(&(task)->itk_lock_data)
495 #define itk_unlock(task) lck_mtx_unlock(&(task)->itk_lock_data)
496
497 /* task clear return wait flags */
498 #define TCRW_CLEAR_INITIAL_WAIT 0x1
499 #define TCRW_CLEAR_FINAL_WAIT 0x2
500 #define TCRW_CLEAR_ALL_WAIT (TCRW_CLEAR_INITIAL_WAIT | TCRW_CLEAR_FINAL_WAIT)
501
502 #define TASK_REFERENCE_LEAK_DEBUG 0
503
504 extern zone_t task_zone;
505
506 #if TASK_REFERENCE_LEAK_DEBUG
507 extern void task_reference_internal(task_t task);
508 extern os_ref_count_t task_deallocate_internal(task_t task);
509 #else
510 #define task_reference_internal(task) \
511 MACRO_BEGIN \
512 zone_require(task, task_zone); \
513 os_ref_retain(&(task)->ref_count); \
514 MACRO_END
515 #define task_deallocate_internal(task) os_ref_release(&(task)->ref_count)
516 #endif
517
518 #define task_reference(task) \
519 MACRO_BEGIN \
520 if ((task) != TASK_NULL) \
521 task_reference_internal(task); \
522 MACRO_END
523
524 extern kern_return_t kernel_task_create(
525 task_t task,
526 vm_offset_t map_base,
527 vm_size_t map_size,
528 task_t *child);
529
530 /* Initialize task module */
531 extern void task_init(void);
532
533 /* coalition_init() calls this to initialize ledgers before task_init() */
534 extern void init_task_ledgers(void);
535
536 #define current_task_fast() (current_thread()->task)
537 #define current_task() current_task_fast()
538
539 extern bool task_is_driver(task_t task);
540
541 extern lck_attr_t task_lck_attr;
542 extern lck_grp_t task_lck_grp;
543
544 struct task_watchport_elem {
545 task_t twe_task;
546 ipc_port_t twe_port; /* (Space lock) */
547 };
548
549 struct task_watchports {
550 os_refcnt_t tw_refcount; /* (Space lock) */
551 task_t tw_task; /* (Space lock) & tw_refcount == 0 */
552 thread_t tw_thread; /* (Space lock) & tw_refcount == 0 */
553 uint32_t tw_elem_array_count; /* (Space lock) */
554 struct task_watchport_elem tw_elem[]; /* (Space lock) & (Portlock) & (mq lock) */
555 };
556
557 #define task_watchports_retain(x) (os_ref_retain(&(x)->tw_refcount))
558 #define task_watchports_release(x) (os_ref_release(&(x)->tw_refcount))
559
560 #define task_watchport_elem_init(elem, task, port) \
561 do { \
562 (elem)->twe_task = (task); \
563 (elem)->twe_port = (port); \
564 } while(0)
565
566 #define task_watchport_elem_clear(elem) task_watchport_elem_init((elem), NULL, NULL)
567
568 extern void
569 task_add_turnstile_watchports(
570 task_t task,
571 thread_t thread,
572 ipc_port_t *portwatch_ports,
573 uint32_t portwatch_count);
574
575 extern void
576 task_watchport_elem_deallocate(
577 struct task_watchport_elem *watchport_elem);
578
579 extern boolean_t
580 task_has_watchports(task_t task);
581
582 #else /* MACH_KERNEL_PRIVATE */
583
584 __BEGIN_DECLS
585
586 extern task_t current_task(void);
587
588 extern void task_reference(task_t task);
589 extern bool task_is_driver(task_t task);
590
591 #define TF_NONE 0
592
593 #define TWF_NONE 0
594 #define TRW_LRETURNWAIT 0x01 /* task is waiting for fork/posix_spawn/exec to complete */
595 #define TRW_LRETURNWAITER 0x02 /* task is waiting for TRW_LRETURNWAIT to get cleared */
596
597 /* task clear return wait flags */
598 #define TCRW_CLEAR_INITIAL_WAIT 0x1
599 #define TCRW_CLEAR_FINAL_WAIT 0x2
600 #define TCRW_CLEAR_ALL_WAIT (TCRW_CLEAR_INITIAL_WAIT | TCRW_CLEAR_FINAL_WAIT)
601
602
603 #define TPF_NONE 0
604 #define TPF_EXEC_COPY 0x00000002 /* task is the new copy of an exec */
605
606
607 __END_DECLS
608
609 #endif /* MACH_KERNEL_PRIVATE */
610
611 __BEGIN_DECLS
612
613 #ifdef KERNEL_PRIVATE
614 extern boolean_t task_is_app_suspended(task_t task);
615 #endif
616
617 #ifdef XNU_KERNEL_PRIVATE
618
619 /* Hold all threads in a task */
620 extern kern_return_t task_hold(
621 task_t task);
622
623 /* Wait for task to stop running, either just to get off CPU or to cease being runnable */
624 extern kern_return_t task_wait(
625 task_t task,
626 boolean_t until_not_runnable);
627
628 /* Release hold on all threads in a task */
629 extern kern_return_t task_release(
630 task_t task);
631
632 /* Suspend/resume a task where the kernel owns the suspend count */
633 extern kern_return_t task_suspend_internal( task_t task);
634 extern kern_return_t task_resume_internal( task_t task);
635
636 /* Suspends a task by placing a hold on its threads */
637 extern kern_return_t task_pidsuspend(
638 task_t task);
639
640 /* Resumes a previously paused task */
641 extern kern_return_t task_pidresume(
642 task_t task);
643
644 extern kern_return_t task_send_trace_memory(
645 task_t task,
646 uint32_t pid,
647 uint64_t uniqueid);
648
649 extern void task_remove_turnstile_watchports(
650 task_t task);
651
652 extern void task_transfer_turnstile_watchports(
653 task_t old_task,
654 task_t new_task,
655 thread_t new_thread);
656
657 #if DEVELOPMENT || DEBUG
658
659 extern kern_return_t task_disconnect_page_mappings(
660 task_t task);
661 #endif
662
663 extern void tasks_system_suspend(boolean_t suspend);
664
665 #if CONFIG_FREEZE
666
667 /* Freeze a task's resident pages */
668 extern kern_return_t task_freeze(
669 task_t task,
670 uint32_t *purgeable_count,
671 uint32_t *wired_count,
672 uint32_t *clean_count,
673 uint32_t *dirty_count,
674 uint32_t dirty_budget,
675 uint32_t *shared_count,
676 int *freezer_error_code,
677 boolean_t eval_only);
678
679 /* Thaw a currently frozen task */
680 extern kern_return_t task_thaw(
681 task_t task);
682
683 #endif /* CONFIG_FREEZE */
684
685 /* Halt all other threads in the current task */
686 extern kern_return_t task_start_halt(
687 task_t task);
688
689 /* Wait for other threads to halt and free halting task resources */
690 extern void task_complete_halt(
691 task_t task);
692
693 extern kern_return_t task_terminate_internal(
694 task_t task);
695
696 extern kern_return_t task_create_internal(
697 task_t parent_task,
698 coalition_t *parent_coalitions,
699 boolean_t inherit_memory,
700 boolean_t is_64bit,
701 boolean_t is_64bit_data,
702 uint32_t flags,
703 uint32_t procflags,
704 uint8_t t_returnwaitflags,
705 task_t *child_task); /* OUT */
706
707 extern kern_return_t task_set_special_port_internal(
708 task_t task,
709 int which,
710 ipc_port_t port);
711
712 extern kern_return_t task_info(
713 task_t task,
714 task_flavor_t flavor,
715 task_info_t task_info_out,
716 mach_msg_type_number_t *task_info_count);
717
718 extern void task_power_info_locked(
719 task_t task,
720 task_power_info_t info,
721 gpu_energy_data_t gpu_energy,
722 task_power_info_v2_t infov2,
723 uint64_t *runnable_time);
724
725 extern uint64_t task_gpu_utilisation(
726 task_t task);
727
728 extern uint64_t task_energy(
729 task_t task);
730
731 extern uint64_t task_cpu_ptime(
732 task_t task);
733 extern void task_update_cpu_time_qos_stats(
734 task_t task,
735 uint64_t *eqos_stats,
736 uint64_t *rqos_stats);
737
738 extern void task_vtimer_set(
739 task_t task,
740 integer_t which);
741
742 extern void task_vtimer_clear(
743 task_t task,
744 integer_t which);
745
746 extern void task_vtimer_update(
747 task_t task,
748 integer_t which,
749 uint32_t *microsecs);
750
751 #define TASK_VTIMER_USER 0x01
752 #define TASK_VTIMER_PROF 0x02
753 #define TASK_VTIMER_RLIM 0x04
754
755 extern void task_set_64bit(
756 task_t task,
757 boolean_t is_64bit,
758 boolean_t is_64bit_data);
759
760 extern boolean_t task_get_64bit_data(
761 task_t task);
762
763 extern void task_set_platform_binary(
764 task_t task,
765 boolean_t is_platform);
766 extern bool task_set_ca_client_wi(
767 task_t task,
768 boolean_t ca_client_wi);
769
770 extern void task_set_dyld_info(
771 task_t task,
772 mach_vm_address_t addr,
773 mach_vm_size_t size);
774
775 extern void task_set_mach_header_address(
776 task_t task,
777 mach_vm_address_t addr);
778
779 /* Get number of activations in a task */
780 extern int get_task_numacts(
781 task_t task);
782
783 struct label;
784 extern kern_return_t task_collect_crash_info(
785 task_t task,
786 #if CONFIG_MACF
787 struct label *crash_label,
788 #endif
789 int is_corpse_fork);
790 void task_port_notify(mach_msg_header_t *msg);
791 void task_wait_till_threads_terminate_locked(task_t task);
792
793 /* JMM - should just be temporary (implementation in bsd_kern still) */
794 extern void set_bsdtask_info(task_t, void *);
795 extern uint32_t set_task_loadTag(task_t task, uint32_t loadTag);
796 extern vm_map_t get_task_map_reference(task_t);
797 extern vm_map_t swap_task_map(task_t, thread_t, vm_map_t);
798 extern pmap_t get_task_pmap(task_t);
799 extern uint64_t get_task_resident_size(task_t);
800 extern uint64_t get_task_compressed(task_t);
801 extern uint64_t get_task_resident_max(task_t);
802 extern uint64_t get_task_phys_footprint(task_t);
803 #if CONFIG_LEDGER_INTERVAL_MAX
804 extern uint64_t get_task_phys_footprint_interval_max(task_t, int reset);
805 #endif /* CONFIG_FOOTPRINT_INTERVAL_MAX */
806 extern uint64_t get_task_phys_footprint_lifetime_max(task_t);
807 extern uint64_t get_task_phys_footprint_limit(task_t);
808 extern uint64_t get_task_purgeable_size(task_t);
809 extern uint64_t get_task_cpu_time(task_t);
810 extern uint64_t get_task_dispatchqueue_offset(task_t);
811 extern uint64_t get_task_dispatchqueue_serialno_offset(task_t);
812 extern uint64_t get_task_dispatchqueue_label_offset(task_t);
813 extern uint64_t get_task_uniqueid(task_t task);
814 extern int get_task_version(task_t task);
815
816 extern uint64_t get_task_internal(task_t);
817 extern uint64_t get_task_internal_compressed(task_t);
818 extern uint64_t get_task_purgeable_nonvolatile(task_t);
819 extern uint64_t get_task_purgeable_nonvolatile_compressed(task_t);
820 extern uint64_t get_task_iokit_mapped(task_t);
821 extern uint64_t get_task_alternate_accounting(task_t);
822 extern uint64_t get_task_alternate_accounting_compressed(task_t);
823 extern uint64_t get_task_memory_region_count(task_t);
824 extern uint64_t get_task_page_table(task_t);
825 extern uint64_t get_task_network_nonvolatile(task_t);
826 extern uint64_t get_task_network_nonvolatile_compressed(task_t);
827 extern uint64_t get_task_wired_mem(task_t);
828 extern uint32_t get_task_loadTag(task_t task);
829
830 extern kern_return_t task_convert_phys_footprint_limit(int, int *);
831 extern kern_return_t task_set_phys_footprint_limit_internal(task_t, int, int *, boolean_t, boolean_t);
832 extern kern_return_t task_get_phys_footprint_limit(task_t task, int *limit_mb);
833
834 /* Jetsam memlimit attributes */
835 extern boolean_t task_get_memlimit_is_active(task_t task);
836 extern boolean_t task_get_memlimit_is_fatal(task_t task);
837 extern void task_set_memlimit_is_active(task_t task, boolean_t memlimit_is_active);
838 extern void task_set_memlimit_is_fatal(task_t task, boolean_t memlimit_is_fatal);
839 extern boolean_t task_has_triggered_exc_resource(task_t task, boolean_t memlimit_is_active);
840 extern void task_mark_has_triggered_exc_resource(task_t task, boolean_t memlimit_is_active);
841
842 extern void task_set_thread_limit(task_t task, uint16_t thread_limit);
843
844
845 extern boolean_t is_kerneltask(task_t task);
846 extern boolean_t is_corpsetask(task_t task);
847
848 extern kern_return_t check_actforsig(task_t task, thread_t thread, int setast);
849
850 extern kern_return_t machine_task_get_state(
851 task_t task,
852 int flavor,
853 thread_state_t state,
854 mach_msg_type_number_t *state_count);
855
856 extern kern_return_t machine_task_set_state(
857 task_t task,
858 int flavor,
859 thread_state_t state,
860 mach_msg_type_number_t state_count);
861
862 extern void machine_task_terminate(task_t task);
863
864 struct _task_ledger_indices {
865 int cpu_time;
866 int tkm_private;
867 int tkm_shared;
868 int phys_mem;
869 int wired_mem;
870 int internal;
871 int iokit_mapped;
872 int alternate_accounting;
873 int alternate_accounting_compressed;
874 int page_table;
875 int phys_footprint;
876 int internal_compressed;
877 int purgeable_volatile;
878 int purgeable_nonvolatile;
879 int purgeable_volatile_compressed;
880 int purgeable_nonvolatile_compressed;
881 int tagged_nofootprint;
882 int tagged_footprint;
883 int tagged_nofootprint_compressed;
884 int tagged_footprint_compressed;
885 int network_volatile;
886 int network_nonvolatile;
887 int network_volatile_compressed;
888 int network_nonvolatile_compressed;
889 int media_nofootprint;
890 int media_footprint;
891 int media_nofootprint_compressed;
892 int media_footprint_compressed;
893 int graphics_nofootprint;
894 int graphics_footprint;
895 int graphics_nofootprint_compressed;
896 int graphics_footprint_compressed;
897 int neural_nofootprint;
898 int neural_footprint;
899 int neural_nofootprint_compressed;
900 int neural_footprint_compressed;
901 int platform_idle_wakeups;
902 int interrupt_wakeups;
903 #if CONFIG_SCHED_SFI
904 int sfi_wait_times[MAX_SFI_CLASS_ID];
905 #endif /* CONFIG_SCHED_SFI */
906 int cpu_time_billed_to_me;
907 int cpu_time_billed_to_others;
908 int physical_writes;
909 int logical_writes;
910 int logical_writes_to_external;
911 int energy_billed_to_me;
912 int energy_billed_to_others;
913 #if DEBUG || DEVELOPMENT
914 int pages_grabbed;
915 int pages_grabbed_kern;
916 int pages_grabbed_iopl;
917 int pages_grabbed_upl;
918 #endif
919 };
920 extern struct _task_ledger_indices task_ledgers;
921
922 /* requires task to be unlocked, returns a referenced thread */
923 thread_t task_findtid(task_t task, uint64_t tid);
924 int pid_from_task(task_t task);
925
926 extern kern_return_t task_wakeups_monitor_ctl(task_t task, uint32_t *rate_hz, int32_t *flags);
927 extern kern_return_t task_cpu_usage_monitor_ctl(task_t task, uint32_t *flags);
928 extern void task_rollup_accounting_info(task_t new_task, task_t parent_task);
929 extern kern_return_t task_io_monitor_ctl(task_t task, uint32_t *flags);
930 extern void task_set_did_exec_flag(task_t task);
931 extern void task_clear_exec_copy_flag(task_t task);
932 extern boolean_t task_is_exec_copy(task_t);
933 extern boolean_t task_did_exec(task_t task);
934 #ifdef CONFIG_32BIT_TELEMETRY
935 extern boolean_t task_consume_32bit_log_flag(task_t task);
936 extern void task_set_32bit_log_flag(task_t task);
937 #endif /* CONFIG_32BIT_TELEMETRY */
938 extern boolean_t task_is_active(task_t task);
939 extern boolean_t task_is_halting(task_t task);
940 extern void task_clear_return_wait(task_t task, uint32_t flags);
941 extern void task_wait_to_return(void) __attribute__((noreturn));
942 extern event_t task_get_return_wait_event(task_t task);
943
944 extern void task_atm_reset(task_t task);
945 extern void task_bank_reset(task_t task);
946 extern void task_bank_init(task_t task);
947
948 #if CONFIG_ARCADE
949 extern void task_prep_arcade(task_t task, thread_t thread);
950 #endif /* CONFIG_ARCADE */
951
952 extern int task_pid(task_t task);
953 extern boolean_t task_has_assertions(task_t task);
954 /* End task_policy */
955
956 extern void task_set_gpu_denied(task_t task, boolean_t denied);
957 extern boolean_t task_is_gpu_denied(task_t task);
958
959 extern queue_head_t * task_io_user_clients(task_t task);
960 extern void task_set_message_app_suspended(task_t task, boolean_t enable);
961
962 extern void task_copy_fields_for_exec(task_t dst_task, task_t src_task);
963
964 extern void task_copy_vmobjects(task_t task, vm_object_query_t query, int len, int64_t* num);
965
966 #if __has_feature(ptrauth_calls)
967 extern bool task_is_pac_exception_fatal(task_t task);
968 extern void task_set_pac_exception_fatal_flag(task_t task);
969 #endif /*__has_feature(ptrauth_calls)*/
970
971 #endif /* XNU_KERNEL_PRIVATE */
972
973 #ifdef KERNEL_PRIVATE
974
975 extern void *get_bsdtask_info(task_t);
976 extern void *get_bsdthreadtask_info(thread_t);
977 extern void task_bsdtask_kill(task_t);
978 extern vm_map_t get_task_map(task_t);
979 extern ledger_t get_task_ledger(task_t);
980
981 extern boolean_t get_task_pidsuspended(task_t);
982 extern boolean_t get_task_suspended(task_t);
983 extern boolean_t get_task_frozen(task_t);
984
985 /* Convert from a task to a port */
986 extern ipc_port_t convert_task_to_port(task_t);
987 extern ipc_port_t convert_task_name_to_port(task_name_t);
988 extern ipc_port_t convert_task_inspect_to_port(task_inspect_t);
989 extern ipc_port_t convert_task_suspension_token_to_port(task_suspension_token_t task);
990
991 /* Convert from a port (in this case, an SO right to a task's resume port) to a task. */
992 extern task_suspension_token_t convert_port_to_task_suspension_token(ipc_port_t port);
993
994 extern boolean_t task_suspension_notify(mach_msg_header_t *);
995
996 #define TASK_WRITE_IMMEDIATE 0x1
997 #define TASK_WRITE_DEFERRED 0x2
998 #define TASK_WRITE_INVALIDATED 0x4
999 #define TASK_WRITE_METADATA 0x8
1000 extern void task_update_logical_writes(task_t task, uint32_t io_size, int flags, void *vp);
1001
1002 #if CONFIG_SECLUDED_MEMORY
1003 extern void task_set_can_use_secluded_mem(
1004 task_t task,
1005 boolean_t can_use_secluded_mem);
1006 extern void task_set_could_use_secluded_mem(
1007 task_t task,
1008 boolean_t could_use_secluded_mem);
1009 extern void task_set_could_also_use_secluded_mem(
1010 task_t task,
1011 boolean_t could_also_use_secluded_mem);
1012 extern boolean_t task_can_use_secluded_mem(
1013 task_t task,
1014 boolean_t is_allocate);
1015 extern boolean_t task_could_use_secluded_mem(task_t task);
1016 extern boolean_t task_could_also_use_secluded_mem(task_t task);
1017 #endif /* CONFIG_SECLUDED_MEMORY */
1018
1019 extern void task_set_darkwake_mode(task_t, boolean_t);
1020 extern boolean_t task_get_darkwake_mode(task_t);
1021
1022 #if __arm64__
1023 extern void task_set_legacy_footprint(task_t task);
1024 extern void task_set_extra_footprint_limit(task_t task);
1025 extern void task_set_ios13extended_footprint_limit(task_t task);
1026 #endif /* __arm64__ */
1027
1028 #if CONFIG_MACF
1029 extern struct label *get_task_crash_label(task_t task);
1030 #endif /* CONFIG_MACF */
1031
1032 extern int get_task_cdhash(task_t task, char cdhash[]);
1033
1034 #endif /* KERNEL_PRIVATE */
1035
1036 extern task_t kernel_task;
1037
1038 extern void task_deallocate(
1039 task_t task);
1040
1041 extern void task_name_deallocate(
1042 task_name_t task_name);
1043
1044 extern void task_inspect_deallocate(
1045 task_inspect_t task_inspect);
1046
1047 extern void task_suspension_token_deallocate(
1048 task_suspension_token_t token);
1049
1050 extern boolean_t task_self_region_footprint(void);
1051 extern void task_self_region_footprint_set(boolean_t newval);
1052 extern void task_ledgers_footprint(ledger_t ledger,
1053 ledger_amount_t *ledger_resident,
1054 ledger_amount_t *ledger_compressed);
1055 extern void task_set_memory_ownership_transfer(
1056 task_t task,
1057 boolean_t value);
1058
1059 __END_DECLS
1060
1061 #endif /* _KERN_TASK_H_ */