]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/task.h
xnu-6153.11.26.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 #endif /* __arm64__ */
431 unsigned int task_region_footprint:1;
432 unsigned int task_has_crossed_thread_limit:1;
433 uint32_t exec_token;
434 /*
435 * A task's coalition set is "adopted" in task_create_internal
436 * and unset in task_deallocate_internal, so each array member
437 * can be referenced without the task lock.
438 * Note: these fields are protected by coalition->lock,
439 * not the task lock.
440 */
441 coalition_t coalition[COALITION_NUM_TYPES];
442 queue_chain_t task_coalition[COALITION_NUM_TYPES];
443 uint64_t dispatchqueue_offset;
444
445 #if DEVELOPMENT || DEBUG
446 boolean_t task_unnested;
447 int task_disconnected_count;
448 #endif
449
450 #if HYPERVISOR
451 void *hv_task_target; /* hypervisor virtual machine object associated with this task */
452 #endif /* HYPERVISOR */
453
454 #if CONFIG_SECLUDED_MEMORY
455 uint8_t task_can_use_secluded_mem;
456 uint8_t task_could_use_secluded_mem;
457 uint8_t task_could_also_use_secluded_mem;
458 uint8_t task_suppressed_secluded;
459 #endif /* CONFIG_SECLUDED_MEMORY */
460
461 task_exc_guard_behavior_t task_exc_guard;
462
463 queue_head_t io_user_clients;
464
465 mach_vm_address_t mach_header_vm_address;
466
467 uint32_t loadTag; /* dext ID used for logging identity */
468 };
469
470 /*
471 * EXC_GUARD default delivery behavior for optional Mach port and VM guards.
472 * Applied to new tasks at creation time.
473 */
474 extern task_exc_guard_behavior_t task_exc_guard_default;
475
476 extern kern_return_t
477 task_violated_guard(mach_exception_code_t, mach_exception_subcode_t, void *);
478
479 #define task_lock(task) lck_mtx_lock(&(task)->lock)
480 #define task_lock_assert_owned(task) LCK_MTX_ASSERT(&(task)->lock, LCK_MTX_ASSERT_OWNED)
481 #define task_lock_try(task) lck_mtx_try_lock(&(task)->lock)
482 #define task_unlock(task) lck_mtx_unlock(&(task)->lock)
483
484 #define task_objq_lock_init(task) lck_mtx_init(&(task)->task_objq_lock, &vm_object_lck_grp, &vm_object_lck_attr)
485 #define task_objq_lock(task) lck_mtx_lock(&(task)->task_objq_lock)
486 #define task_objq_lock_assert_owned(task) LCK_MTX_ASSERT(&(task)->task_objq_lock, LCK_MTX_ASSERT_OWNED)
487 #define task_objq_lock_try(task) lck_mtx_try_lock(&(task)->task_objq_lock)
488 #define task_objq_unlock(task) lck_mtx_unlock(&(task)->task_objq_lock)
489
490 #define itk_lock_init(task) lck_mtx_init(&(task)->itk_lock_data, &ipc_lck_grp, &ipc_lck_attr)
491 #define itk_lock_destroy(task) lck_mtx_destroy(&(task)->itk_lock_data, &ipc_lck_grp)
492 #define itk_lock(task) lck_mtx_lock(&(task)->itk_lock_data)
493 #define itk_unlock(task) lck_mtx_unlock(&(task)->itk_lock_data)
494
495 /* task clear return wait flags */
496 #define TCRW_CLEAR_INITIAL_WAIT 0x1
497 #define TCRW_CLEAR_FINAL_WAIT 0x2
498 #define TCRW_CLEAR_ALL_WAIT (TCRW_CLEAR_INITIAL_WAIT | TCRW_CLEAR_FINAL_WAIT)
499
500 #define TASK_REFERENCE_LEAK_DEBUG 0
501
502 #if TASK_REFERENCE_LEAK_DEBUG
503 extern void task_reference_internal(task_t task);
504 extern os_ref_count_t task_deallocate_internal(task_t task);
505 #else
506 #define task_reference_internal(task) os_ref_retain(&(task)->ref_count)
507 #define task_deallocate_internal(task) os_ref_release(&(task)->ref_count)
508 #endif
509
510 #define task_reference(task) \
511 MACRO_BEGIN \
512 if ((task) != TASK_NULL) \
513 task_reference_internal(task); \
514 MACRO_END
515
516 extern kern_return_t kernel_task_create(
517 task_t task,
518 vm_offset_t map_base,
519 vm_size_t map_size,
520 task_t *child);
521
522 /* Initialize task module */
523 extern void task_init(void);
524
525 /* coalition_init() calls this to initialize ledgers before task_init() */
526 extern void init_task_ledgers(void);
527
528 #define current_task_fast() (current_thread()->task)
529 #define current_task() current_task_fast()
530
531 extern bool task_is_driver(task_t task);
532
533 extern lck_attr_t task_lck_attr;
534 extern lck_grp_t task_lck_grp;
535
536 struct task_watchport_elem {
537 task_t twe_task;
538 ipc_port_t twe_port; /* (Space lock) */
539 };
540
541 struct task_watchports {
542 os_refcnt_t tw_refcount; /* (Space lock) */
543 task_t tw_task; /* (Space lock) & tw_refcount == 0 */
544 thread_t tw_thread; /* (Space lock) & tw_refcount == 0 */
545 uint32_t tw_elem_array_count; /* (Space lock) */
546 struct task_watchport_elem tw_elem[]; /* (Space lock) & (Portlock) & (mq lock) */
547 };
548
549 #define task_watchports_retain(x) (os_ref_retain(&(x)->tw_refcount))
550 #define task_watchports_release(x) (os_ref_release(&(x)->tw_refcount))
551
552 #define task_watchport_elem_init(elem, task, port) \
553 do { \
554 (elem)->twe_task = (task); \
555 (elem)->twe_port = (port); \
556 } while(0)
557
558 #define task_watchport_elem_clear(elem) task_watchport_elem_init((elem), NULL, NULL)
559
560 extern void
561 task_add_turnstile_watchports(
562 task_t task,
563 thread_t thread,
564 ipc_port_t *portwatch_ports,
565 uint32_t portwatch_count);
566
567 extern void
568 task_watchport_elem_deallocate(
569 struct task_watchport_elem *watchport_elem);
570
571 extern boolean_t
572 task_has_watchports(task_t task);
573
574 #else /* MACH_KERNEL_PRIVATE */
575
576 __BEGIN_DECLS
577
578 extern task_t current_task(void);
579
580 extern void task_reference(task_t task);
581 extern bool task_is_driver(task_t task);
582
583 #define TF_NONE 0
584
585 #define TWF_NONE 0
586 #define TRW_LRETURNWAIT 0x01 /* task is waiting for fork/posix_spawn/exec to complete */
587 #define TRW_LRETURNWAITER 0x02 /* task is waiting for TRW_LRETURNWAIT to get cleared */
588
589 /* task clear return wait flags */
590 #define TCRW_CLEAR_INITIAL_WAIT 0x1
591 #define TCRW_CLEAR_FINAL_WAIT 0x2
592 #define TCRW_CLEAR_ALL_WAIT (TCRW_CLEAR_INITIAL_WAIT | TCRW_CLEAR_FINAL_WAIT)
593
594
595 #define TPF_NONE 0
596 #define TPF_EXEC_COPY 0x00000002 /* task is the new copy of an exec */
597
598
599 __END_DECLS
600
601 #endif /* MACH_KERNEL_PRIVATE */
602
603 __BEGIN_DECLS
604
605 #ifdef KERNEL_PRIVATE
606 extern boolean_t task_is_app_suspended(task_t task);
607 #endif
608
609 #ifdef XNU_KERNEL_PRIVATE
610
611 /* Hold all threads in a task */
612 extern kern_return_t task_hold(
613 task_t task);
614
615 /* Wait for task to stop running, either just to get off CPU or to cease being runnable */
616 extern kern_return_t task_wait(
617 task_t task,
618 boolean_t until_not_runnable);
619
620 /* Release hold on all threads in a task */
621 extern kern_return_t task_release(
622 task_t task);
623
624 /* Suspend/resume a task where the kernel owns the suspend count */
625 extern kern_return_t task_suspend_internal( task_t task);
626 extern kern_return_t task_resume_internal( task_t task);
627
628 /* Suspends a task by placing a hold on its threads */
629 extern kern_return_t task_pidsuspend(
630 task_t task);
631
632 /* Resumes a previously paused task */
633 extern kern_return_t task_pidresume(
634 task_t task);
635
636 extern kern_return_t task_send_trace_memory(
637 task_t task,
638 uint32_t pid,
639 uint64_t uniqueid);
640
641 extern void task_remove_turnstile_watchports(
642 task_t task);
643
644 extern void task_transfer_turnstile_watchports(
645 task_t old_task,
646 task_t new_task,
647 thread_t new_thread);
648
649 #if DEVELOPMENT || DEBUG
650
651 extern kern_return_t task_disconnect_page_mappings(
652 task_t task);
653 #endif
654
655 extern void tasks_system_suspend(boolean_t suspend);
656
657 #if CONFIG_FREEZE
658
659 /* Freeze a task's resident pages */
660 extern kern_return_t task_freeze(
661 task_t task,
662 uint32_t *purgeable_count,
663 uint32_t *wired_count,
664 uint32_t *clean_count,
665 uint32_t *dirty_count,
666 uint32_t dirty_budget,
667 uint32_t *shared_count,
668 int *freezer_error_code,
669 boolean_t eval_only);
670
671 /* Thaw a currently frozen task */
672 extern kern_return_t task_thaw(
673 task_t task);
674
675 #endif /* CONFIG_FREEZE */
676
677 /* Halt all other threads in the current task */
678 extern kern_return_t task_start_halt(
679 task_t task);
680
681 /* Wait for other threads to halt and free halting task resources */
682 extern void task_complete_halt(
683 task_t task);
684
685 extern kern_return_t task_terminate_internal(
686 task_t task);
687
688 extern kern_return_t task_create_internal(
689 task_t parent_task,
690 coalition_t *parent_coalitions,
691 boolean_t inherit_memory,
692 boolean_t is_64bit,
693 boolean_t is_64bit_data,
694 uint32_t flags,
695 uint32_t procflags,
696 uint8_t t_returnwaitflags,
697 task_t *child_task); /* OUT */
698
699 extern kern_return_t task_info(
700 task_t task,
701 task_flavor_t flavor,
702 task_info_t task_info_out,
703 mach_msg_type_number_t *task_info_count);
704
705 extern void task_power_info_locked(
706 task_t task,
707 task_power_info_t info,
708 gpu_energy_data_t gpu_energy,
709 task_power_info_v2_t infov2,
710 uint64_t *runnable_time);
711
712 extern uint64_t task_gpu_utilisation(
713 task_t task);
714
715 extern uint64_t task_energy(
716 task_t task);
717
718 extern uint64_t task_cpu_ptime(
719 task_t task);
720 extern void task_update_cpu_time_qos_stats(
721 task_t task,
722 uint64_t *eqos_stats,
723 uint64_t *rqos_stats);
724
725 extern void task_vtimer_set(
726 task_t task,
727 integer_t which);
728
729 extern void task_vtimer_clear(
730 task_t task,
731 integer_t which);
732
733 extern void task_vtimer_update(
734 task_t task,
735 integer_t which,
736 uint32_t *microsecs);
737
738 #define TASK_VTIMER_USER 0x01
739 #define TASK_VTIMER_PROF 0x02
740 #define TASK_VTIMER_RLIM 0x04
741
742 extern void task_set_64bit(
743 task_t task,
744 boolean_t is_64bit,
745 boolean_t is_64bit_data);
746
747 extern boolean_t task_get_64bit_data(
748 task_t task);
749
750 extern void task_set_platform_binary(
751 task_t task,
752 boolean_t is_platform);
753 extern bool task_set_ca_client_wi(
754 task_t task,
755 boolean_t ca_client_wi);
756
757 extern void task_set_dyld_info(
758 task_t task,
759 mach_vm_address_t addr,
760 mach_vm_size_t size);
761
762 extern void task_set_mach_header_address(
763 task_t task,
764 mach_vm_address_t addr);
765
766 /* Get number of activations in a task */
767 extern int get_task_numacts(
768 task_t task);
769
770 struct label;
771 extern kern_return_t task_collect_crash_info(
772 task_t task,
773 #if CONFIG_MACF
774 struct label *crash_label,
775 #endif
776 int is_corpse_fork);
777 void task_port_notify(mach_msg_header_t *msg);
778 void task_wait_till_threads_terminate_locked(task_t task);
779
780 /* JMM - should just be temporary (implementation in bsd_kern still) */
781 extern void set_bsdtask_info(task_t, void *);
782 extern uint32_t set_task_loadTag(task_t task, uint32_t loadTag);
783 extern vm_map_t get_task_map_reference(task_t);
784 extern vm_map_t swap_task_map(task_t, thread_t, vm_map_t);
785 extern pmap_t get_task_pmap(task_t);
786 extern uint64_t get_task_resident_size(task_t);
787 extern uint64_t get_task_compressed(task_t);
788 extern uint64_t get_task_resident_max(task_t);
789 extern uint64_t get_task_phys_footprint(task_t);
790 #if CONFIG_LEDGER_INTERVAL_MAX
791 extern uint64_t get_task_phys_footprint_interval_max(task_t, int reset);
792 #endif /* CONFIG_FOOTPRINT_INTERVAL_MAX */
793 extern uint64_t get_task_phys_footprint_lifetime_max(task_t);
794 extern uint64_t get_task_phys_footprint_limit(task_t);
795 extern uint64_t get_task_purgeable_size(task_t);
796 extern uint64_t get_task_cpu_time(task_t);
797 extern uint64_t get_task_dispatchqueue_offset(task_t);
798 extern uint64_t get_task_dispatchqueue_serialno_offset(task_t);
799 extern uint64_t get_task_dispatchqueue_label_offset(task_t);
800 extern uint64_t get_task_uniqueid(task_t task);
801 extern int get_task_version(task_t task);
802
803 extern uint64_t get_task_internal(task_t);
804 extern uint64_t get_task_internal_compressed(task_t);
805 extern uint64_t get_task_purgeable_nonvolatile(task_t);
806 extern uint64_t get_task_purgeable_nonvolatile_compressed(task_t);
807 extern uint64_t get_task_iokit_mapped(task_t);
808 extern uint64_t get_task_alternate_accounting(task_t);
809 extern uint64_t get_task_alternate_accounting_compressed(task_t);
810 extern uint64_t get_task_memory_region_count(task_t);
811 extern uint64_t get_task_page_table(task_t);
812 extern uint64_t get_task_network_nonvolatile(task_t);
813 extern uint64_t get_task_network_nonvolatile_compressed(task_t);
814 extern uint64_t get_task_wired_mem(task_t);
815 extern uint32_t get_task_loadTag(task_t task);
816
817 extern kern_return_t task_convert_phys_footprint_limit(int, int *);
818 extern kern_return_t task_set_phys_footprint_limit_internal(task_t, int, int *, boolean_t, boolean_t);
819 extern kern_return_t task_get_phys_footprint_limit(task_t task, int *limit_mb);
820
821 /* Jetsam memlimit attributes */
822 extern boolean_t task_get_memlimit_is_active(task_t task);
823 extern boolean_t task_get_memlimit_is_fatal(task_t task);
824 extern void task_set_memlimit_is_active(task_t task, boolean_t memlimit_is_active);
825 extern void task_set_memlimit_is_fatal(task_t task, boolean_t memlimit_is_fatal);
826 extern boolean_t task_has_triggered_exc_resource(task_t task, boolean_t memlimit_is_active);
827 extern void task_mark_has_triggered_exc_resource(task_t task, boolean_t memlimit_is_active);
828
829 extern void task_set_thread_limit(task_t task, uint16_t thread_limit);
830
831
832 extern boolean_t is_kerneltask(task_t task);
833 extern boolean_t is_corpsetask(task_t task);
834
835 extern kern_return_t check_actforsig(task_t task, thread_t thread, int setast);
836
837 extern kern_return_t machine_task_get_state(
838 task_t task,
839 int flavor,
840 thread_state_t state,
841 mach_msg_type_number_t *state_count);
842
843 extern kern_return_t machine_task_set_state(
844 task_t task,
845 int flavor,
846 thread_state_t state,
847 mach_msg_type_number_t state_count);
848
849 extern void machine_task_terminate(task_t task);
850
851 struct _task_ledger_indices {
852 int cpu_time;
853 int tkm_private;
854 int tkm_shared;
855 int phys_mem;
856 int wired_mem;
857 int internal;
858 int iokit_mapped;
859 int alternate_accounting;
860 int alternate_accounting_compressed;
861 int page_table;
862 int phys_footprint;
863 int internal_compressed;
864 int purgeable_volatile;
865 int purgeable_nonvolatile;
866 int purgeable_volatile_compressed;
867 int purgeable_nonvolatile_compressed;
868 int tagged_nofootprint;
869 int tagged_footprint;
870 int tagged_nofootprint_compressed;
871 int tagged_footprint_compressed;
872 int network_volatile;
873 int network_nonvolatile;
874 int network_volatile_compressed;
875 int network_nonvolatile_compressed;
876 int media_nofootprint;
877 int media_footprint;
878 int media_nofootprint_compressed;
879 int media_footprint_compressed;
880 int graphics_nofootprint;
881 int graphics_footprint;
882 int graphics_nofootprint_compressed;
883 int graphics_footprint_compressed;
884 int neural_nofootprint;
885 int neural_footprint;
886 int neural_nofootprint_compressed;
887 int neural_footprint_compressed;
888 int platform_idle_wakeups;
889 int interrupt_wakeups;
890 #if CONFIG_SCHED_SFI
891 int sfi_wait_times[MAX_SFI_CLASS_ID];
892 #endif /* CONFIG_SCHED_SFI */
893 int cpu_time_billed_to_me;
894 int cpu_time_billed_to_others;
895 int physical_writes;
896 int logical_writes;
897 int logical_writes_to_external;
898 int energy_billed_to_me;
899 int energy_billed_to_others;
900 #if DEBUG || DEVELOPMENT
901 int pages_grabbed;
902 int pages_grabbed_kern;
903 int pages_grabbed_iopl;
904 int pages_grabbed_upl;
905 #endif
906 };
907 extern struct _task_ledger_indices task_ledgers;
908
909 /* requires task to be unlocked, returns a referenced thread */
910 thread_t task_findtid(task_t task, uint64_t tid);
911 int pid_from_task(task_t task);
912
913 extern kern_return_t task_wakeups_monitor_ctl(task_t task, uint32_t *rate_hz, int32_t *flags);
914 extern kern_return_t task_cpu_usage_monitor_ctl(task_t task, uint32_t *flags);
915 extern void task_rollup_accounting_info(task_t new_task, task_t parent_task);
916 extern kern_return_t task_io_monitor_ctl(task_t task, uint32_t *flags);
917 extern void task_set_did_exec_flag(task_t task);
918 extern void task_clear_exec_copy_flag(task_t task);
919 extern boolean_t task_is_exec_copy(task_t);
920 extern boolean_t task_did_exec(task_t task);
921 #ifdef CONFIG_32BIT_TELEMETRY
922 extern boolean_t task_consume_32bit_log_flag(task_t task);
923 extern void task_set_32bit_log_flag(task_t task);
924 #endif /* CONFIG_32BIT_TELEMETRY */
925 extern boolean_t task_is_active(task_t task);
926 extern boolean_t task_is_halting(task_t task);
927 extern void task_clear_return_wait(task_t task, uint32_t flags);
928 extern void task_wait_to_return(void) __attribute__((noreturn));
929 extern event_t task_get_return_wait_event(task_t task);
930
931 extern void task_atm_reset(task_t task);
932 extern void task_bank_reset(task_t task);
933 extern void task_bank_init(task_t task);
934
935 #if CONFIG_ARCADE
936 extern void task_prep_arcade(task_t task, thread_t thread);
937 #endif /* CONFIG_ARCADE */
938
939 extern int task_pid(task_t task);
940 extern boolean_t task_has_assertions(task_t task);
941 /* End task_policy */
942
943 extern void task_set_gpu_denied(task_t task, boolean_t denied);
944 extern boolean_t task_is_gpu_denied(task_t task);
945
946 extern queue_head_t * task_io_user_clients(task_t task);
947 extern void task_set_message_app_suspended(task_t task, boolean_t enable);
948
949 extern void task_copy_fields_for_exec(task_t dst_task, task_t src_task);
950
951 extern void task_copy_vmobjects(task_t task, vm_object_query_t query, int len, int64_t* num);
952
953 #endif /* XNU_KERNEL_PRIVATE */
954
955 #ifdef KERNEL_PRIVATE
956
957 extern void *get_bsdtask_info(task_t);
958 extern void *get_bsdthreadtask_info(thread_t);
959 extern void task_bsdtask_kill(task_t);
960 extern vm_map_t get_task_map(task_t);
961 extern ledger_t get_task_ledger(task_t);
962
963 extern boolean_t get_task_pidsuspended(task_t);
964 extern boolean_t get_task_suspended(task_t);
965 extern boolean_t get_task_frozen(task_t);
966
967 /* Convert from a task to a port */
968 extern ipc_port_t convert_task_to_port(task_t);
969 extern ipc_port_t convert_task_name_to_port(task_name_t);
970 extern ipc_port_t convert_task_inspect_to_port(task_inspect_t);
971 extern ipc_port_t convert_task_suspension_token_to_port(task_suspension_token_t task);
972
973 /* Convert from a port (in this case, an SO right to a task's resume port) to a task. */
974 extern task_suspension_token_t convert_port_to_task_suspension_token(ipc_port_t port);
975
976 extern boolean_t task_suspension_notify(mach_msg_header_t *);
977
978 #define TASK_WRITE_IMMEDIATE 0x1
979 #define TASK_WRITE_DEFERRED 0x2
980 #define TASK_WRITE_INVALIDATED 0x4
981 #define TASK_WRITE_METADATA 0x8
982 extern void task_update_logical_writes(task_t task, uint32_t io_size, int flags, void *vp);
983
984 #if CONFIG_SECLUDED_MEMORY
985 extern void task_set_can_use_secluded_mem(
986 task_t task,
987 boolean_t can_use_secluded_mem);
988 extern void task_set_could_use_secluded_mem(
989 task_t task,
990 boolean_t could_use_secluded_mem);
991 extern void task_set_could_also_use_secluded_mem(
992 task_t task,
993 boolean_t could_also_use_secluded_mem);
994 extern boolean_t task_can_use_secluded_mem(
995 task_t task,
996 boolean_t is_allocate);
997 extern boolean_t task_could_use_secluded_mem(task_t task);
998 extern boolean_t task_could_also_use_secluded_mem(task_t task);
999 #endif /* CONFIG_SECLUDED_MEMORY */
1000
1001 extern void task_set_darkwake_mode(task_t, boolean_t);
1002 extern boolean_t task_get_darkwake_mode(task_t);
1003
1004 #if __arm64__
1005 extern void task_set_legacy_footprint(task_t task);
1006 extern void task_set_extra_footprint_limit(task_t task);
1007 #endif /* __arm64__ */
1008
1009 #if CONFIG_MACF
1010 extern struct label *get_task_crash_label(task_t task);
1011 #endif /* CONFIG_MACF */
1012
1013 extern int get_task_cdhash(task_t task, char cdhash[]);
1014
1015 #endif /* KERNEL_PRIVATE */
1016
1017 extern task_t kernel_task;
1018
1019 extern void task_deallocate(
1020 task_t task);
1021
1022 extern void task_name_deallocate(
1023 task_name_t task_name);
1024
1025 extern void task_inspect_deallocate(
1026 task_inspect_t task_inspect);
1027
1028 extern void task_suspension_token_deallocate(
1029 task_suspension_token_t token);
1030
1031 extern boolean_t task_self_region_footprint(void);
1032 extern void task_self_region_footprint_set(boolean_t newval);
1033 extern void task_ledgers_footprint(ledger_t ledger,
1034 ledger_amount_t *ledger_resident,
1035 ledger_amount_t *ledger_compressed);
1036 extern void task_set_memory_ownership_transfer(
1037 task_t task,
1038 boolean_t value);
1039
1040 __END_DECLS
1041
1042 #endif /* _KERN_TASK_H_ */