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