]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/task.h
xnu-6153.61.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
275 /*
276 * Task is running within a 64-bit address space.
277 */
278 #define task_has_64Bit_addr(task) \
279 (((task)->t_flags & TF_64B_ADDR) != 0)
280 #define task_set_64Bit_addr(task) \
281 ((task)->t_flags |= TF_64B_ADDR)
282 #define task_clear_64Bit_addr(task) \
283 ((task)->t_flags &= ~TF_64B_ADDR)
284
285 /*
286 * Task is using 64-bit machine state.
287 */
288 #define task_has_64Bit_data(task) \
289 (((task)->t_flags & TF_64B_DATA) != 0)
290 #define task_set_64Bit_data(task) \
291 ((task)->t_flags |= TF_64B_DATA)
292 #define task_clear_64Bit_data(task) \
293 ((task)->t_flags &= ~TF_64B_DATA)
294
295 #define task_is_a_corpse(task) \
296 (((task)->t_flags & TF_CORPSE) != 0)
297
298 #define task_set_corpse(task) \
299 ((task)->t_flags |= TF_CORPSE)
300
301 #define task_corpse_pending_report(task) \
302 (((task)->t_flags & TF_PENDING_CORPSE) != 0)
303
304 #define task_set_corpse_pending_report(task) \
305 ((task)->t_flags |= TF_PENDING_CORPSE)
306
307 #define task_clear_corpse_pending_report(task) \
308 ((task)->t_flags &= ~TF_PENDING_CORPSE)
309
310 #define task_is_a_corpse_fork(task) \
311 (((task)->t_flags & TF_CORPSE_FORK) != 0)
312
313 uint32_t t_procflags; /* general-purpose task flags protected by proc_lock (PL) */
314 #define TPF_NONE 0
315 #define TPF_DID_EXEC 0x00000001 /* task has been execed to a new task */
316 #define TPF_EXEC_COPY 0x00000002 /* task is the new copy of an exec */
317 #ifdef CONFIG_32BIT_TELEMETRY
318 #define TPF_LOG_32BIT_TELEMETRY 0x00000004 /* task should log identifying information */
319 #endif
320
321 #define task_did_exec_internal(task) \
322 (((task)->t_procflags & TPF_DID_EXEC) != 0)
323
324 #define task_is_exec_copy_internal(task) \
325 (((task)->t_procflags & TPF_EXEC_COPY) != 0)
326
327 uint8_t t_returnwaitflags;
328 #define TWF_NONE 0
329 #define TRW_LRETURNWAIT 0x01 /* task is waiting for fork/posix_spawn/exec to complete */
330 #define TRW_LRETURNWAITER 0x02 /* task is waiting for TRW_LRETURNWAIT to get cleared */
331
332 mach_vm_address_t all_image_info_addr; /* dyld __all_image_info */
333 mach_vm_size_t all_image_info_size; /* section location and size */
334
335 #if KPC
336 #define TASK_KPC_FORCED_ALL_CTRS 0x2 /* Bit in "t_kpc" signifying this task forced all counters */
337 uint32_t t_kpc; /* kpc flags */
338 #endif /* KPC */
339
340 boolean_t pidsuspended; /* pid_suspend called; no threads can execute */
341 boolean_t frozen; /* frozen; private resident pages committed to swap */
342 boolean_t changing_freeze_state; /* in the process of freezing or thawing */
343 uint16_t policy_ru_cpu :4,
344 policy_ru_cpu_ext :4,
345 applied_ru_cpu :4,
346 applied_ru_cpu_ext :4;
347 uint8_t rusage_cpu_flags;
348 uint8_t rusage_cpu_percentage; /* Task-wide CPU limit percentage */
349 uint8_t rusage_cpu_perthr_percentage; /* Per-thread CPU limit percentage */
350 #if MACH_ASSERT
351 int8_t suspends_outstanding; /* suspends this task performed in excess of resumes */
352 #endif
353 uint64_t rusage_cpu_interval; /* Task-wide CPU limit interval */
354 uint64_t rusage_cpu_perthr_interval; /* Per-thread CPU limit interval */
355 uint64_t rusage_cpu_deadline;
356 thread_call_t rusage_cpu_callt;
357 #if CONFIG_EMBEDDED
358 queue_head_t task_watchers; /* app state watcher threads */
359 int num_taskwatchers;
360 int watchapplying;
361 #endif /* CONFIG_EMBEDDED */
362
363 #if CONFIG_ATM
364 struct atm_task_descriptor *atm_context; /* pointer to per task atm descriptor */
365 #endif
366 struct bank_task *bank_context; /* pointer to per task bank structure */
367
368 #if IMPORTANCE_INHERITANCE
369 struct ipc_importance_task *task_imp_base; /* Base of IPC importance chain */
370 #endif /* IMPORTANCE_INHERITANCE */
371
372 vm_extmod_statistics_data_t extmod_statistics;
373
374 struct task_requested_policy requested_policy;
375 struct task_effective_policy effective_policy;
376
377 /*
378 * Can be merged with imp_donor bits, once the IMPORTANCE_INHERITANCE macro goes away.
379 */
380 uint32_t low_mem_notified_warn :1, /* warning low memory notification is sent to the task */
381 low_mem_notified_critical :1, /* critical low memory notification is sent to the task */
382 purged_memory_warn :1, /* purgeable memory of the task is purged for warning level pressure */
383 purged_memory_critical :1, /* purgeable memory of the task is purged for critical level pressure */
384 low_mem_privileged_listener :1, /* if set, task would like to know about pressure changes before other tasks on the system */
385 mem_notify_reserved :27; /* reserved for future use */
386
387 uint32_t memlimit_is_active :1, /* if set, use active attributes, otherwise use inactive attributes */
388 memlimit_is_fatal :1, /* if set, exceeding current memlimit will prove fatal to the task */
389 memlimit_active_exc_resource :1, /* if set, suppress exc_resource exception when task exceeds active memory limit */
390 memlimit_inactive_exc_resource :1, /* if set, suppress exc_resource exception when task exceeds inactive memory limit */
391 memlimit_attrs_reserved :28; /* reserved for future use */
392
393 io_stat_info_t task_io_stats;
394
395 struct task_writes_counters task_writes_counters_internal;
396 struct task_writes_counters task_writes_counters_external;
397
398 /*
399 * The cpu_time_qos_stats fields are protected by the task lock
400 */
401 struct _cpu_time_qos_stats cpu_time_eqos_stats;
402 struct _cpu_time_qos_stats cpu_time_rqos_stats;
403
404 /* Statistics accumulated for terminated threads from this task */
405 uint32_t task_timer_wakeups_bin_1;
406 uint32_t task_timer_wakeups_bin_2;
407 uint64_t task_gpu_ns;
408 uint64_t task_energy;
409
410 #if MONOTONIC
411 /* Read and written under task_lock */
412 struct mt_task task_monotonic;
413 #endif /* MONOTONIC */
414
415 uint8_t task_can_transfer_memory_ownership;
416 uint8_t task_objects_disowning;
417 uint8_t task_objects_disowned;
418 /* # of purgeable volatile VM objects owned by this task: */
419 int task_volatile_objects;
420 /* # of purgeable but not volatile VM objects owned by this task: */
421 int task_nonvolatile_objects;
422 int task_owned_objects;
423 queue_head_t task_objq;
424 decl_lck_mtx_data(, task_objq_lock); /* protects "task_objq" */
425
426 unsigned int task_thread_limit:16;
427 #if __arm64__
428 unsigned int task_legacy_footprint:1;
429 unsigned int task_extra_footprint_limit:1;
430 unsigned int task_ios13extended_footprint_limit:1;
431 #endif /* __arm64__ */
432 unsigned int task_region_footprint:1;
433 unsigned int task_has_crossed_thread_limit:1;
434 uint32_t exec_token;
435 /*
436 * A task's coalition set is "adopted" in task_create_internal
437 * and unset in task_deallocate_internal, so each array member
438 * can be referenced without the task lock.
439 * Note: these fields are protected by coalition->lock,
440 * not the task lock.
441 */
442 coalition_t coalition[COALITION_NUM_TYPES];
443 queue_chain_t task_coalition[COALITION_NUM_TYPES];
444 uint64_t dispatchqueue_offset;
445
446 #if DEVELOPMENT || DEBUG
447 boolean_t task_unnested;
448 int task_disconnected_count;
449 #endif
450
451 #if HYPERVISOR
452 void *hv_task_target; /* hypervisor virtual machine object associated with this task */
453 #endif /* HYPERVISOR */
454
455 #if CONFIG_SECLUDED_MEMORY
456 uint8_t task_can_use_secluded_mem;
457 uint8_t task_could_use_secluded_mem;
458 uint8_t task_could_also_use_secluded_mem;
459 uint8_t task_suppressed_secluded;
460 #endif /* CONFIG_SECLUDED_MEMORY */
461
462 task_exc_guard_behavior_t task_exc_guard;
463
464 queue_head_t io_user_clients;
465
466 mach_vm_address_t mach_header_vm_address;
467
468 uint32_t loadTag; /* dext ID used for logging identity */
469 };
470
471 /*
472 * EXC_GUARD default delivery behavior for optional Mach port and VM guards.
473 * Applied to new tasks at creation time.
474 */
475 extern task_exc_guard_behavior_t task_exc_guard_default;
476
477 extern kern_return_t
478 task_violated_guard(mach_exception_code_t, mach_exception_subcode_t, void *);
479
480 #define task_lock(task) lck_mtx_lock(&(task)->lock)
481 #define task_lock_assert_owned(task) LCK_MTX_ASSERT(&(task)->lock, LCK_MTX_ASSERT_OWNED)
482 #define task_lock_try(task) lck_mtx_try_lock(&(task)->lock)
483 #define task_unlock(task) lck_mtx_unlock(&(task)->lock)
484
485 #define task_objq_lock_init(task) lck_mtx_init(&(task)->task_objq_lock, &vm_object_lck_grp, &vm_object_lck_attr)
486 #define task_objq_lock(task) lck_mtx_lock(&(task)->task_objq_lock)
487 #define task_objq_lock_assert_owned(task) LCK_MTX_ASSERT(&(task)->task_objq_lock, LCK_MTX_ASSERT_OWNED)
488 #define task_objq_lock_try(task) lck_mtx_try_lock(&(task)->task_objq_lock)
489 #define task_objq_unlock(task) lck_mtx_unlock(&(task)->task_objq_lock)
490
491 #define itk_lock_init(task) lck_mtx_init(&(task)->itk_lock_data, &ipc_lck_grp, &ipc_lck_attr)
492 #define itk_lock_destroy(task) lck_mtx_destroy(&(task)->itk_lock_data, &ipc_lck_grp)
493 #define itk_lock(task) lck_mtx_lock(&(task)->itk_lock_data)
494 #define itk_unlock(task) lck_mtx_unlock(&(task)->itk_lock_data)
495
496 /* task clear return wait flags */
497 #define TCRW_CLEAR_INITIAL_WAIT 0x1
498 #define TCRW_CLEAR_FINAL_WAIT 0x2
499 #define TCRW_CLEAR_ALL_WAIT (TCRW_CLEAR_INITIAL_WAIT | TCRW_CLEAR_FINAL_WAIT)
500
501 #define TASK_REFERENCE_LEAK_DEBUG 0
502
503 #if TASK_REFERENCE_LEAK_DEBUG
504 extern void task_reference_internal(task_t task);
505 extern os_ref_count_t task_deallocate_internal(task_t task);
506 #else
507 #define task_reference_internal(task) os_ref_retain(&(task)->ref_count)
508 #define task_deallocate_internal(task) os_ref_release(&(task)->ref_count)
509 #endif
510
511 #define task_reference(task) \
512 MACRO_BEGIN \
513 if ((task) != TASK_NULL) \
514 task_reference_internal(task); \
515 MACRO_END
516
517 extern kern_return_t kernel_task_create(
518 task_t task,
519 vm_offset_t map_base,
520 vm_size_t map_size,
521 task_t *child);
522
523 /* Initialize task module */
524 extern void task_init(void);
525
526 /* coalition_init() calls this to initialize ledgers before task_init() */
527 extern void init_task_ledgers(void);
528
529 #define current_task_fast() (current_thread()->task)
530 #define current_task() current_task_fast()
531
532 extern bool task_is_driver(task_t task);
533
534 extern lck_attr_t task_lck_attr;
535 extern lck_grp_t task_lck_grp;
536
537 struct task_watchport_elem {
538 task_t twe_task;
539 ipc_port_t twe_port; /* (Space lock) */
540 };
541
542 struct task_watchports {
543 os_refcnt_t tw_refcount; /* (Space lock) */
544 task_t tw_task; /* (Space lock) & tw_refcount == 0 */
545 thread_t tw_thread; /* (Space lock) & tw_refcount == 0 */
546 uint32_t tw_elem_array_count; /* (Space lock) */
547 struct task_watchport_elem tw_elem[]; /* (Space lock) & (Portlock) & (mq lock) */
548 };
549
550 #define task_watchports_retain(x) (os_ref_retain(&(x)->tw_refcount))
551 #define task_watchports_release(x) (os_ref_release(&(x)->tw_refcount))
552
553 #define task_watchport_elem_init(elem, task, port) \
554 do { \
555 (elem)->twe_task = (task); \
556 (elem)->twe_port = (port); \
557 } while(0)
558
559 #define task_watchport_elem_clear(elem) task_watchport_elem_init((elem), NULL, NULL)
560
561 extern void
562 task_add_turnstile_watchports(
563 task_t task,
564 thread_t thread,
565 ipc_port_t *portwatch_ports,
566 uint32_t portwatch_count);
567
568 extern void
569 task_watchport_elem_deallocate(
570 struct task_watchport_elem *watchport_elem);
571
572 extern boolean_t
573 task_has_watchports(task_t task);
574
575 #else /* MACH_KERNEL_PRIVATE */
576
577 __BEGIN_DECLS
578
579 extern task_t current_task(void);
580
581 extern void task_reference(task_t task);
582 extern bool task_is_driver(task_t task);
583
584 #define TF_NONE 0
585
586 #define TWF_NONE 0
587 #define TRW_LRETURNWAIT 0x01 /* task is waiting for fork/posix_spawn/exec to complete */
588 #define TRW_LRETURNWAITER 0x02 /* task is waiting for TRW_LRETURNWAIT to get cleared */
589
590 /* task clear return wait flags */
591 #define TCRW_CLEAR_INITIAL_WAIT 0x1
592 #define TCRW_CLEAR_FINAL_WAIT 0x2
593 #define TCRW_CLEAR_ALL_WAIT (TCRW_CLEAR_INITIAL_WAIT | TCRW_CLEAR_FINAL_WAIT)
594
595
596 #define TPF_NONE 0
597 #define TPF_EXEC_COPY 0x00000002 /* task is the new copy of an exec */
598
599
600 __END_DECLS
601
602 #endif /* MACH_KERNEL_PRIVATE */
603
604 __BEGIN_DECLS
605
606 #ifdef KERNEL_PRIVATE
607 extern boolean_t task_is_app_suspended(task_t task);
608 #endif
609
610 #ifdef XNU_KERNEL_PRIVATE
611
612 /* Hold all threads in a task */
613 extern kern_return_t task_hold(
614 task_t task);
615
616 /* Wait for task to stop running, either just to get off CPU or to cease being runnable */
617 extern kern_return_t task_wait(
618 task_t task,
619 boolean_t until_not_runnable);
620
621 /* Release hold on all threads in a task */
622 extern kern_return_t task_release(
623 task_t task);
624
625 /* Suspend/resume a task where the kernel owns the suspend count */
626 extern kern_return_t task_suspend_internal( task_t task);
627 extern kern_return_t task_resume_internal( task_t task);
628
629 /* Suspends a task by placing a hold on its threads */
630 extern kern_return_t task_pidsuspend(
631 task_t task);
632
633 /* Resumes a previously paused task */
634 extern kern_return_t task_pidresume(
635 task_t task);
636
637 extern kern_return_t task_send_trace_memory(
638 task_t task,
639 uint32_t pid,
640 uint64_t uniqueid);
641
642 extern void task_remove_turnstile_watchports(
643 task_t task);
644
645 extern void task_transfer_turnstile_watchports(
646 task_t old_task,
647 task_t new_task,
648 thread_t new_thread);
649
650 #if DEVELOPMENT || DEBUG
651
652 extern kern_return_t task_disconnect_page_mappings(
653 task_t task);
654 #endif
655
656 extern void tasks_system_suspend(boolean_t suspend);
657
658 #if CONFIG_FREEZE
659
660 /* Freeze a task's resident pages */
661 extern kern_return_t task_freeze(
662 task_t task,
663 uint32_t *purgeable_count,
664 uint32_t *wired_count,
665 uint32_t *clean_count,
666 uint32_t *dirty_count,
667 uint32_t dirty_budget,
668 uint32_t *shared_count,
669 int *freezer_error_code,
670 boolean_t eval_only);
671
672 /* Thaw a currently frozen task */
673 extern kern_return_t task_thaw(
674 task_t task);
675
676 #endif /* CONFIG_FREEZE */
677
678 /* Halt all other threads in the current task */
679 extern kern_return_t task_start_halt(
680 task_t task);
681
682 /* Wait for other threads to halt and free halting task resources */
683 extern void task_complete_halt(
684 task_t task);
685
686 extern kern_return_t task_terminate_internal(
687 task_t task);
688
689 extern kern_return_t task_create_internal(
690 task_t parent_task,
691 coalition_t *parent_coalitions,
692 boolean_t inherit_memory,
693 boolean_t is_64bit,
694 boolean_t is_64bit_data,
695 uint32_t flags,
696 uint32_t procflags,
697 uint8_t t_returnwaitflags,
698 task_t *child_task); /* OUT */
699
700 extern kern_return_t task_set_special_port_internal(
701 task_t task,
702 int which,
703 ipc_port_t port);
704
705 extern kern_return_t task_info(
706 task_t task,
707 task_flavor_t flavor,
708 task_info_t task_info_out,
709 mach_msg_type_number_t *task_info_count);
710
711 extern void task_power_info_locked(
712 task_t task,
713 task_power_info_t info,
714 gpu_energy_data_t gpu_energy,
715 task_power_info_v2_t infov2,
716 uint64_t *runnable_time);
717
718 extern uint64_t task_gpu_utilisation(
719 task_t task);
720
721 extern uint64_t task_energy(
722 task_t task);
723
724 extern uint64_t task_cpu_ptime(
725 task_t task);
726 extern void task_update_cpu_time_qos_stats(
727 task_t task,
728 uint64_t *eqos_stats,
729 uint64_t *rqos_stats);
730
731 extern void task_vtimer_set(
732 task_t task,
733 integer_t which);
734
735 extern void task_vtimer_clear(
736 task_t task,
737 integer_t which);
738
739 extern void task_vtimer_update(
740 task_t task,
741 integer_t which,
742 uint32_t *microsecs);
743
744 #define TASK_VTIMER_USER 0x01
745 #define TASK_VTIMER_PROF 0x02
746 #define TASK_VTIMER_RLIM 0x04
747
748 extern void task_set_64bit(
749 task_t task,
750 boolean_t is_64bit,
751 boolean_t is_64bit_data);
752
753 extern boolean_t task_get_64bit_data(
754 task_t task);
755
756 extern void task_set_platform_binary(
757 task_t task,
758 boolean_t is_platform);
759 extern bool task_set_ca_client_wi(
760 task_t task,
761 boolean_t ca_client_wi);
762
763 extern void task_set_dyld_info(
764 task_t task,
765 mach_vm_address_t addr,
766 mach_vm_size_t size);
767
768 extern void task_set_mach_header_address(
769 task_t task,
770 mach_vm_address_t addr);
771
772 /* Get number of activations in a task */
773 extern int get_task_numacts(
774 task_t task);
775
776 struct label;
777 extern kern_return_t task_collect_crash_info(
778 task_t task,
779 #if CONFIG_MACF
780 struct label *crash_label,
781 #endif
782 int is_corpse_fork);
783 void task_port_notify(mach_msg_header_t *msg);
784 void task_wait_till_threads_terminate_locked(task_t task);
785
786 /* JMM - should just be temporary (implementation in bsd_kern still) */
787 extern void set_bsdtask_info(task_t, void *);
788 extern uint32_t set_task_loadTag(task_t task, uint32_t loadTag);
789 extern vm_map_t get_task_map_reference(task_t);
790 extern vm_map_t swap_task_map(task_t, thread_t, vm_map_t);
791 extern pmap_t get_task_pmap(task_t);
792 extern uint64_t get_task_resident_size(task_t);
793 extern uint64_t get_task_compressed(task_t);
794 extern uint64_t get_task_resident_max(task_t);
795 extern uint64_t get_task_phys_footprint(task_t);
796 #if CONFIG_LEDGER_INTERVAL_MAX
797 extern uint64_t get_task_phys_footprint_interval_max(task_t, int reset);
798 #endif /* CONFIG_FOOTPRINT_INTERVAL_MAX */
799 extern uint64_t get_task_phys_footprint_lifetime_max(task_t);
800 extern uint64_t get_task_phys_footprint_limit(task_t);
801 extern uint64_t get_task_purgeable_size(task_t);
802 extern uint64_t get_task_cpu_time(task_t);
803 extern uint64_t get_task_dispatchqueue_offset(task_t);
804 extern uint64_t get_task_dispatchqueue_serialno_offset(task_t);
805 extern uint64_t get_task_dispatchqueue_label_offset(task_t);
806 extern uint64_t get_task_uniqueid(task_t task);
807 extern int get_task_version(task_t task);
808
809 extern uint64_t get_task_internal(task_t);
810 extern uint64_t get_task_internal_compressed(task_t);
811 extern uint64_t get_task_purgeable_nonvolatile(task_t);
812 extern uint64_t get_task_purgeable_nonvolatile_compressed(task_t);
813 extern uint64_t get_task_iokit_mapped(task_t);
814 extern uint64_t get_task_alternate_accounting(task_t);
815 extern uint64_t get_task_alternate_accounting_compressed(task_t);
816 extern uint64_t get_task_memory_region_count(task_t);
817 extern uint64_t get_task_page_table(task_t);
818 extern uint64_t get_task_network_nonvolatile(task_t);
819 extern uint64_t get_task_network_nonvolatile_compressed(task_t);
820 extern uint64_t get_task_wired_mem(task_t);
821 extern uint32_t get_task_loadTag(task_t task);
822
823 extern kern_return_t task_convert_phys_footprint_limit(int, int *);
824 extern kern_return_t task_set_phys_footprint_limit_internal(task_t, int, int *, boolean_t, boolean_t);
825 extern kern_return_t task_get_phys_footprint_limit(task_t task, int *limit_mb);
826
827 /* Jetsam memlimit attributes */
828 extern boolean_t task_get_memlimit_is_active(task_t task);
829 extern boolean_t task_get_memlimit_is_fatal(task_t task);
830 extern void task_set_memlimit_is_active(task_t task, boolean_t memlimit_is_active);
831 extern void task_set_memlimit_is_fatal(task_t task, boolean_t memlimit_is_fatal);
832 extern boolean_t task_has_triggered_exc_resource(task_t task, boolean_t memlimit_is_active);
833 extern void task_mark_has_triggered_exc_resource(task_t task, boolean_t memlimit_is_active);
834
835 extern void task_set_thread_limit(task_t task, uint16_t thread_limit);
836
837
838 extern boolean_t is_kerneltask(task_t task);
839 extern boolean_t is_corpsetask(task_t task);
840
841 extern kern_return_t check_actforsig(task_t task, thread_t thread, int setast);
842
843 extern kern_return_t machine_task_get_state(
844 task_t task,
845 int flavor,
846 thread_state_t state,
847 mach_msg_type_number_t *state_count);
848
849 extern kern_return_t machine_task_set_state(
850 task_t task,
851 int flavor,
852 thread_state_t state,
853 mach_msg_type_number_t state_count);
854
855 extern void machine_task_terminate(task_t task);
856
857 struct _task_ledger_indices {
858 int cpu_time;
859 int tkm_private;
860 int tkm_shared;
861 int phys_mem;
862 int wired_mem;
863 int internal;
864 int iokit_mapped;
865 int alternate_accounting;
866 int alternate_accounting_compressed;
867 int page_table;
868 int phys_footprint;
869 int internal_compressed;
870 int purgeable_volatile;
871 int purgeable_nonvolatile;
872 int purgeable_volatile_compressed;
873 int purgeable_nonvolatile_compressed;
874 int tagged_nofootprint;
875 int tagged_footprint;
876 int tagged_nofootprint_compressed;
877 int tagged_footprint_compressed;
878 int network_volatile;
879 int network_nonvolatile;
880 int network_volatile_compressed;
881 int network_nonvolatile_compressed;
882 int media_nofootprint;
883 int media_footprint;
884 int media_nofootprint_compressed;
885 int media_footprint_compressed;
886 int graphics_nofootprint;
887 int graphics_footprint;
888 int graphics_nofootprint_compressed;
889 int graphics_footprint_compressed;
890 int neural_nofootprint;
891 int neural_footprint;
892 int neural_nofootprint_compressed;
893 int neural_footprint_compressed;
894 int platform_idle_wakeups;
895 int interrupt_wakeups;
896 #if CONFIG_SCHED_SFI
897 int sfi_wait_times[MAX_SFI_CLASS_ID];
898 #endif /* CONFIG_SCHED_SFI */
899 int cpu_time_billed_to_me;
900 int cpu_time_billed_to_others;
901 int physical_writes;
902 int logical_writes;
903 int logical_writes_to_external;
904 int energy_billed_to_me;
905 int energy_billed_to_others;
906 #if DEBUG || DEVELOPMENT
907 int pages_grabbed;
908 int pages_grabbed_kern;
909 int pages_grabbed_iopl;
910 int pages_grabbed_upl;
911 #endif
912 };
913 extern struct _task_ledger_indices task_ledgers;
914
915 /* requires task to be unlocked, returns a referenced thread */
916 thread_t task_findtid(task_t task, uint64_t tid);
917 int pid_from_task(task_t task);
918
919 extern kern_return_t task_wakeups_monitor_ctl(task_t task, uint32_t *rate_hz, int32_t *flags);
920 extern kern_return_t task_cpu_usage_monitor_ctl(task_t task, uint32_t *flags);
921 extern void task_rollup_accounting_info(task_t new_task, task_t parent_task);
922 extern kern_return_t task_io_monitor_ctl(task_t task, uint32_t *flags);
923 extern void task_set_did_exec_flag(task_t task);
924 extern void task_clear_exec_copy_flag(task_t task);
925 extern boolean_t task_is_exec_copy(task_t);
926 extern boolean_t task_did_exec(task_t task);
927 #ifdef CONFIG_32BIT_TELEMETRY
928 extern boolean_t task_consume_32bit_log_flag(task_t task);
929 extern void task_set_32bit_log_flag(task_t task);
930 #endif /* CONFIG_32BIT_TELEMETRY */
931 extern boolean_t task_is_active(task_t task);
932 extern boolean_t task_is_halting(task_t task);
933 extern void task_clear_return_wait(task_t task, uint32_t flags);
934 extern void task_wait_to_return(void) __attribute__((noreturn));
935 extern event_t task_get_return_wait_event(task_t task);
936
937 extern void task_atm_reset(task_t task);
938 extern void task_bank_reset(task_t task);
939 extern void task_bank_init(task_t task);
940
941 #if CONFIG_ARCADE
942 extern void task_prep_arcade(task_t task, thread_t thread);
943 #endif /* CONFIG_ARCADE */
944
945 extern int task_pid(task_t task);
946 extern boolean_t task_has_assertions(task_t task);
947 /* End task_policy */
948
949 extern void task_set_gpu_denied(task_t task, boolean_t denied);
950 extern boolean_t task_is_gpu_denied(task_t task);
951
952 extern queue_head_t * task_io_user_clients(task_t task);
953 extern void task_set_message_app_suspended(task_t task, boolean_t enable);
954
955 extern void task_copy_fields_for_exec(task_t dst_task, task_t src_task);
956
957 extern void task_copy_vmobjects(task_t task, vm_object_query_t query, int len, int64_t* num);
958
959 #endif /* XNU_KERNEL_PRIVATE */
960
961 #ifdef KERNEL_PRIVATE
962
963 extern void *get_bsdtask_info(task_t);
964 extern void *get_bsdthreadtask_info(thread_t);
965 extern void task_bsdtask_kill(task_t);
966 extern vm_map_t get_task_map(task_t);
967 extern ledger_t get_task_ledger(task_t);
968
969 extern boolean_t get_task_pidsuspended(task_t);
970 extern boolean_t get_task_suspended(task_t);
971 extern boolean_t get_task_frozen(task_t);
972
973 /* Convert from a task to a port */
974 extern ipc_port_t convert_task_to_port(task_t);
975 extern ipc_port_t convert_task_name_to_port(task_name_t);
976 extern ipc_port_t convert_task_inspect_to_port(task_inspect_t);
977 extern ipc_port_t convert_task_suspension_token_to_port(task_suspension_token_t task);
978
979 /* Convert from a port (in this case, an SO right to a task's resume port) to a task. */
980 extern task_suspension_token_t convert_port_to_task_suspension_token(ipc_port_t port);
981
982 extern boolean_t task_suspension_notify(mach_msg_header_t *);
983
984 #define TASK_WRITE_IMMEDIATE 0x1
985 #define TASK_WRITE_DEFERRED 0x2
986 #define TASK_WRITE_INVALIDATED 0x4
987 #define TASK_WRITE_METADATA 0x8
988 extern void task_update_logical_writes(task_t task, uint32_t io_size, int flags, void *vp);
989
990 #if CONFIG_SECLUDED_MEMORY
991 extern void task_set_can_use_secluded_mem(
992 task_t task,
993 boolean_t can_use_secluded_mem);
994 extern void task_set_could_use_secluded_mem(
995 task_t task,
996 boolean_t could_use_secluded_mem);
997 extern void task_set_could_also_use_secluded_mem(
998 task_t task,
999 boolean_t could_also_use_secluded_mem);
1000 extern boolean_t task_can_use_secluded_mem(
1001 task_t task,
1002 boolean_t is_allocate);
1003 extern boolean_t task_could_use_secluded_mem(task_t task);
1004 extern boolean_t task_could_also_use_secluded_mem(task_t task);
1005 #endif /* CONFIG_SECLUDED_MEMORY */
1006
1007 extern void task_set_darkwake_mode(task_t, boolean_t);
1008 extern boolean_t task_get_darkwake_mode(task_t);
1009
1010 #if __arm64__
1011 extern void task_set_legacy_footprint(task_t task);
1012 extern void task_set_extra_footprint_limit(task_t task);
1013 extern void task_set_ios13extended_footprint_limit(task_t task);
1014 #endif /* __arm64__ */
1015
1016 #if CONFIG_MACF
1017 extern struct label *get_task_crash_label(task_t task);
1018 #endif /* CONFIG_MACF */
1019
1020 extern int get_task_cdhash(task_t task, char cdhash[]);
1021
1022 #endif /* KERNEL_PRIVATE */
1023
1024 extern task_t kernel_task;
1025
1026 extern void task_deallocate(
1027 task_t task);
1028
1029 extern void task_name_deallocate(
1030 task_name_t task_name);
1031
1032 extern void task_inspect_deallocate(
1033 task_inspect_t task_inspect);
1034
1035 extern void task_suspension_token_deallocate(
1036 task_suspension_token_t token);
1037
1038 extern boolean_t task_self_region_footprint(void);
1039 extern void task_self_region_footprint_set(boolean_t newval);
1040 extern void task_ledgers_footprint(ledger_t ledger,
1041 ledger_amount_t *ledger_resident,
1042 ledger_amount_t *ledger_compressed);
1043 extern void task_set_memory_ownership_transfer(
1044 task_t task,
1045 boolean_t value);
1046
1047 __END_DECLS
1048
1049 #endif /* _KERN_TASK_H_ */