2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * @OSF_FREE_COPYRIGHT@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
60 * Author: Avadis Tevanian, Jr.
62 * This file contains the structure definitions for tasks.
66 * Copyright (c) 1993 The University of Utah and
67 * the Computer Systems Laboratory (CSL). All rights reserved.
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.
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.
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.
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,
88 * Copyright (c) 2005 SPARTA, Inc.
94 #include <kern/kern_types.h>
95 #include <mach/mach_types.h>
96 #include <sys/cdefs.h>
98 #ifdef MACH_KERNEL_PRIVATE
100 #include <mach/boolean.h>
101 #include <mach/port.h>
102 #include <mach/time_value.h>
103 #include <mach/message.h>
104 #include <mach/mach_param.h>
105 #include <mach/task_info.h>
106 #include <mach/exception_types.h>
107 #include <mach/vm_statistics.h>
108 #include <machine/task.h>
110 #include <kern/cpu_data.h>
111 #include <kern/queue.h>
112 #include <kern/exception.h>
113 #include <kern/lock.h>
114 #include <security/_label.h>
115 #include <ipc/ipc_labelh.h>
116 #endif /* MACH_KERNEL_PRIVATE */
118 #ifdef XNU_KERNEL_PRIVATE
120 /* defns for task->rsu_controldata */
121 #define TASK_POLICY_CPU_RESOURCE_USAGE 0
122 #define TASK_POLICY_WIREDMEM_RESOURCE_USAGE 1
123 #define TASK_POLICY_VIRTUALMEM_RESOURCE_USAGE 2
124 #define TASK_POLICY_DISK_RESOURCE_USAGE 3
125 #define TASK_POLICY_NETWORK_RESOURCE_USAGE 4
126 #define TASK_POLICY_POWER_RESOURCE_USAGE 5
128 #define TASK_POLICY_RESOURCE_USAGE_COUNT 6
131 * Process Action and Policy bit definitions
133 The bit defns of the policy states
134 64 60 56 52 48 44 40 36 32 28 24 20 16 12 8 0
135 |----|-----|----|----|----|----|----|----|----|----|----|----|----|----|--------|
136 |RFU | RFU | PWR| NET| DSK| CPU| VM | WM | LVM| RFU| CPU| NET| GPU| DSK| BGRND |
137 |----|-----|----|----|----|----|----|----|----|----|----|----|----|----|--------|
138 |<----------- RESOURCE USAGE -------->|< LOWSRC>|<-HARDWARE ACCESS->|BackGrnd|
139 |----|-----|----|----|----|----|----|----|----|----|----|----|----|----|--------|
144 #define TASK_POLICY_BACKGROUND_ATTRIBUTE_NONE 0x00
145 #define TASK_POLICY_BACKGROUND_ATTRIBUTE_LOWPRI 0x01
146 #define TASK_POLICY_BACKGROUND_ATTRIBUTE_DISKTHROTTLE 0x02
147 #define TASK_POLICY_BACKGROUND_ATTRIBUTE_NETTHROTTLE 0x04
148 #define TASK_POLICY_BACKGROUND_ATTRIBUTE_NOGPU 0x08
150 #define TASK_POLICY_BACKGROUND_ATTRIBUTE_ALL 0x0F
151 #else /* CONFIG_EMBEDDED */
152 #define TASK_POLICY_BACKGROUND_ATTRIBUTE_ALL 0x07
153 #endif /* CONFIG_EMBEDDED */
154 #define TASK_POLICY_BACKGROUND_ATTRIBUTE_DEFAULT TASK_POLICY_BACKGROUND_ATTRIBUTE_ALL
156 /* Hardware disk access attributes, bit different as it should reflect IOPOL_XXX */
157 #define TASK_POLICY_HWACCESS_DISK_ATTRIBUTE_NONE 0x00
158 #define TASK_POLICY_HWACCESS_DISK_ATTRIBUTE_NORMAL 0x01
159 #define TASK_POLICY_HWACCESS_DISK_ATTRIBUTE_PASSIVE 0x02
160 #define TASK_POLICY_HWACCESS_DISK_ATTRIBUTE_THROTTLE 0x03
161 #define TASK_POLICY_HWACCESS_DISK_ATTRIBUTE_DEFAULT TASK_POLICY_HWACCESS_DISK_ATTRIBUTE_NORMAL
163 /* Hardware disk access attributes */
164 #define TASK_POLICY_HWACCESS_GPU_ATTRIBUTE_NONE 0x00
165 #define TASK_POLICY_HWACCESS_GPU_ATTRIBUTE_NORMAL 0x00
166 #define TASK_POLICY_HWACCESS_GPU_ATTRIBUTE_FULLACCESS 0x00
167 #define TASK_POLICY_HWACCESS_GPU_ATTRIBUTE_NOACCESS 0x01
168 #define TASK_POLICY_HWACCESS_GPU_ATTRIBUTE_DEFAULT 0x00
170 /* Hardware Network access attributes */
171 #define TASK_POLICY_HWACCESS_NET_ATTRIBUTE_NONE 0x00
172 #define TASK_POLICY_HWACCESS_NET_ATTRIBUTE_NORMAL 0x00
173 #define TASK_POLICY_HWACCESS_NET_ATTRIBUTE_THROTTLE 0x01
174 #define TASK_POLICY_HWACCESS_NET_ATTRIBUTE_DEFAULT 0x00
176 /* Hardware CPU access attributes */
177 #define TASK_POLICY_HWACCESS_CPU_ATTRIBUTE_NONE 0x00
178 #define TASK_POLICY_HWACCESS_CPU_ATTRIBUTE_NORMAL 0x00
179 #define TASK_POLICY_HWACCESS_CPU_ATTRIBUTE_ALL 0x00
180 #define TASK_POLICY_HWACCESS_CPU_ATTRIBUTE_ONE 0x01
181 #define TASK_POLICY_HWACCESS_CPU_ATTRIBUTE_LLCACHE 0x02
182 #define TASK_POLICY_HWACCESS_CPU_ATTRIBUTE_DEFAULT 0x00
184 /* Resource usage/low resource attributes */
185 #define TASK_POLICY_RESOURCE_ATTRIBUTE_NONE 0x00
186 #define TASK_POLICY_RESOURCE_ATTRIBUTE_THROTTLE 0x01
187 #define TASK_POLICY_RESOURCE_ATTRIBUTE_SUSPEND 0x02
188 #define TASK_POLICY_RESOURCE_ATTRIBUTE_TERMINATE 0x03
189 #define TASK_POLICY_RESOURCE_ATTRIBUTE_NOTIFY 0x04
190 #define TASK_POLICY_RESOURCE_ATTRIBUTE_DEFAULT 0x00
192 #endif /* XNU_KERNEL_PRIVATE */
194 #ifdef MACH_KERNEL_PRIVATE
196 typedef struct process_policy
{
199 ru_power
:4, /* Resource Usage Power */
200 ru_net
:4, /* Resource Usage Network */
201 ru_disk
:4, /* Resource Usage Disk */
202 ru_cpu
:4, /* Resource Usage CPU */
203 ru_virtmem
:4, /* Resource Usage VM */
204 ru_wiredmem
:4,/* Resource Usage Wired Memory */
205 low_vm
:4, /* Low Virtual Memory */
207 hw_cpu
:4, /* HW Access to CPU */
208 hw_net
:4, /* HW Access to Network */
209 hw_gpu
:4, /* HW Access to GPU */
210 hw_disk
:4, /* HW Access to Disk */
211 hw_bg
:8; /* Darwin Background Policy */
214 #include <kern/thread.h>
216 extern process_policy_t default_task_proc_policy
; /* init value for the process policy attributes */
217 extern process_policy_t default_task_null_policy
; /* none as the value for the process policy attributes */
220 /* Synchronization/destruction information */
221 decl_lck_mtx_data(,lock
) /* Task's lock */
222 uint32_t ref_count
; /* Number of references to me */
223 boolean_t active
; /* Task has not been terminated */
224 boolean_t halting
; /* Task is being halted */
227 vm_map_t map
; /* Address space description */
228 queue_chain_t tasks
; /* global list of tasks */
229 void *user_data
; /* Arbitrary data settable via IPC */
231 /* Threads in this task */
232 queue_head_t threads
;
234 processor_set_t pset_hint
;
235 struct affinity_space
*affinity_space
;
238 uint32_t active_thread_count
;
239 int suspend_count
; /* Internal scheduling only */
241 /* User-visible scheduling information */
242 integer_t user_stop_count
; /* outstanding stops */
246 integer_t priority
; /* base priority for threads */
247 integer_t max_priority
; /* maximum priority for threads */
249 /* Task security and audit tokens */
250 security_token_t sec_token
;
251 audit_token_t audit_token
;
254 uint64_t total_user_time
; /* terminated threads only */
255 uint64_t total_system_time
;
261 decl_lck_mtx_data(,itk_lock_data
)
262 struct ipc_port
*itk_self
; /* not a right, doesn't hold ref */
263 struct ipc_port
*itk_nself
; /* not a right, doesn't hold ref */
264 struct ipc_port
*itk_sself
; /* a send right */
265 struct exception_action exc_actions
[EXC_TYPES_COUNT
];
266 /* a send right each valid element */
267 struct ipc_port
*itk_host
; /* a send right */
268 struct ipc_port
*itk_bootstrap
; /* a send right */
269 struct ipc_port
*itk_seatbelt
; /* a send right */
270 struct ipc_port
*itk_gssd
; /* yet another send right */
271 struct ipc_port
*itk_task_access
; /* and another send right */
272 struct ipc_port
*itk_registered
[TASK_PORT_REGISTER_MAX
];
273 /* all send rights */
275 struct ipc_space
*itk_space
;
277 /* Synchronizer ownership information */
278 queue_head_t semaphore_list
; /* list of owned semaphores */
279 queue_head_t lock_set_list
; /* list of owned lock sets */
280 int semaphores_owned
; /* number of semaphores owned */
281 int lock_sets_owned
; /* number of lock sets owned */
284 struct ipc_port
*wired_ledger_port
;
285 struct ipc_port
*paged_ledger_port
;
286 unsigned int priv_flags
; /* privilege resource flags */
287 #define VM_BACKING_STORE_PRIV 0x1
291 integer_t faults
; /* faults counter */
292 integer_t pageins
; /* pageins counter */
293 integer_t cow_faults
; /* copy on write fault counter */
294 integer_t messages_sent
; /* messages sent counter */
295 integer_t messages_received
; /* messages received counter */
296 integer_t syscalls_mach
; /* mach system call counter */
297 integer_t syscalls_unix
; /* unix system call counter */
298 uint32_t c_switch
; /* total context switches */
299 uint32_t p_switch
; /* total processor switches */
300 uint32_t ps_switch
; /* total pset switches */
302 zinfo_usage_store_t tkm_private
;/* private kmem alloc/free stats (reaped threads) */
303 zinfo_usage_store_t tkm_shared
; /* shared kmem alloc/free stats (reaped threads) */
304 zinfo_usage_t tkm_zinfo
; /* per-task, per-zone usage statistics */
309 struct vm_shared_region
*shared_region
;
310 uint32_t taskFeatures
[2]; /* Special feature for this task */
311 #define tf64BitAddr 0x80000000 /* Task has 64-bit addressing */
312 #define tf64BitData 0x40000000 /* Task has 64-bit data registers */
313 #define task_has_64BitAddr(task) \
314 (((task)->taskFeatures[0] & tf64BitAddr) != 0)
315 #define task_set_64BitAddr(task) \
316 ((task)->taskFeatures[0] |= tf64BitAddr)
317 #define task_clear_64BitAddr(task) \
318 ((task)->taskFeatures[0] &= ~tf64BitAddr)
320 mach_vm_address_t all_image_info_addr
; /* dyld __all_image_info */
321 mach_vm_size_t all_image_info_size
; /* section location and size */
327 #define TASK_PMC_FLAG 0x1 /* Bit in "t_chud" signifying PMC interest */
328 uint32_t t_chud
; /* CHUD flags, used for Shark */
331 process_policy_t ext_actionstate
; /* externally applied actions */
332 process_policy_t ext_policystate
; /* externally defined process policy states*/
333 process_policy_t actionstate
; /* self applied acions */
334 process_policy_t policystate
; /* process wide policy states */
336 uint64_t rsu_controldata
[TASK_POLICY_RESOURCE_USAGE_COUNT
];
338 vm_extmod_statistics_data_t extmod_statistics
;
341 #define task_lock(task) lck_mtx_lock(&(task)->lock)
342 #define task_lock_try(task) lck_mtx_try_lock(&(task)->lock)
343 #define task_unlock(task) lck_mtx_unlock(&(task)->lock)
346 #define maclabel label->lh_label
348 #define tasklabel_lock(task) lh_lock((task)->label)
349 #define tasklabel_unlock(task) lh_unlock((task)->label)
351 extern void tasklabel_lock2(task_t a
, task_t b
);
352 extern void tasklabel_unlock2(task_t a
, task_t b
);
353 #endif /* MAC_MACH */
355 #define itk_lock_init(task) lck_mtx_init(&(task)->itk_lock_data, &ipc_lck_grp, &ipc_lck_attr)
356 #define itk_lock_destroy(task) lck_mtx_destroy(&(task)->itk_lock_data, &ipc_lck_grp)
357 #define itk_lock(task) lck_mtx_lock(&(task)->itk_lock_data)
358 #define itk_unlock(task) lck_mtx_unlock(&(task)->itk_lock_data)
360 #define task_reference_internal(task) \
361 (void)hw_atomic_add(&(task)->ref_count, 1)
363 #define task_deallocate_internal(task) \
364 hw_atomic_sub(&(task)->ref_count, 1)
366 #define task_reference(task) \
368 if ((task) != TASK_NULL) \
369 task_reference_internal(task); \
372 extern kern_return_t
kernel_task_create(
374 vm_offset_t map_base
,
378 /* Initialize task module */
379 extern void task_init(void) __attribute__((section("__TEXT, initcode")));
381 #define current_task_fast() (current_thread()->task)
382 #define current_task() current_task_fast()
384 extern lck_attr_t task_lck_attr
;
385 extern lck_grp_t task_lck_grp
;
387 #else /* MACH_KERNEL_PRIVATE */
391 extern task_t
current_task(void);
393 extern void task_reference(task_t task
);
397 #endif /* MACH_KERNEL_PRIVATE */
401 #ifdef XNU_KERNEL_PRIVATE
403 /* Hold all threads in a task */
404 extern kern_return_t
task_hold(
407 /* Release hold on all threads in a task */
408 extern kern_return_t
task_release(
413 /* Freeze a task's resident pages */
414 extern kern_return_t
task_freeze(
416 uint32_t *purgeable_count
,
417 uint32_t *wired_count
,
418 uint32_t *clean_count
,
419 uint32_t *dirty_count
,
421 boolean_t walk_only
);
423 /* Thaw a currently frozen task */
424 extern kern_return_t
task_thaw(
427 #endif /* CONFIG_FREEZE */
429 /* Halt all other threads in the current task */
430 extern kern_return_t
task_start_halt(
433 /* Wait for other threads to halt and free halting task resources */
434 extern void task_complete_halt(
437 extern kern_return_t
task_terminate_internal(
440 extern kern_return_t
task_create_internal(
442 boolean_t inherit_memory
,
444 task_t
*child_task
); /* OUT */
446 extern kern_return_t
task_importance(
448 integer_t importance
);
450 extern void task_vtimer_set(
454 extern void task_vtimer_clear(
458 extern void task_vtimer_update(
461 uint32_t *microsecs
);
463 #define TASK_VTIMER_USER 0x01
464 #define TASK_VTIMER_PROF 0x02
465 #define TASK_VTIMER_RLIM 0x04
467 extern void task_set_64bit(
471 extern void task_backing_store_privileged(
474 extern void task_set_dyld_info(
476 mach_vm_address_t addr
,
477 mach_vm_size_t size
);
479 /* Get number of activations in a task */
480 extern int get_task_numacts(
483 extern int get_task_numactivethreads(task_t task
);
485 /* JMM - should just be temporary (implementation in bsd_kern still) */
486 extern void set_bsdtask_info(task_t
,void *);
487 extern vm_map_t
get_task_map_reference(task_t
);
488 extern vm_map_t
swap_task_map(task_t
, thread_t
, vm_map_t
, boolean_t
);
489 extern pmap_t
get_task_pmap(task_t
);
490 extern uint64_t get_task_resident_size(task_t
);
492 extern boolean_t
is_kerneltask(task_t task
);
494 extern kern_return_t
check_actforsig(task_t task
, thread_t thread
, int setast
);
496 extern kern_return_t
machine_task_get_state(
499 thread_state_t state
,
500 mach_msg_type_number_t
*state_count
);
502 extern kern_return_t
machine_task_set_state(
505 thread_state_t state
,
506 mach_msg_type_number_t state_count
);
509 int proc_get_task_bg_policy(task_t task
);
510 int proc_get_thread_bg_policy(task_t task
, uint64_t tid
);
511 int proc_get_self_isbackground(void);
512 int proc_get_selfthread_isbackground(void);
514 int proc_get_darwinbgstate(task_t
, uint32_t *);
515 int proc_set_bgtaskpolicy(task_t task
, int intval
);
516 int proc_set1_bgtaskpolicy(task_t task
, int intval
);
517 int proc_set_bgthreadpolicy(task_t task
, uint64_t tid
, int val
);
518 int proc_set1_bgthreadpolicy(task_t task
, uint64_t tid
, int val
);
520 int proc_add_bgtaskpolicy(task_t task
, int val
);
521 int proc_add_bgthreadpolicy(task_t task
, uint64_t tid
, int val
);
522 int proc_remove_bgtaskpolicy(task_t task
, int policy
);
523 int proc_remove_bgthreadpolicy(task_t task
, uint64_t tid
, int val
);
525 int proc_apply_bgtaskpolicy(task_t task
);
526 int proc_apply_bgtaskpolicy_external(task_t task
);
527 int proc_apply_bgtaskpolicy_internal(task_t task
);
528 int proc_apply_bgthreadpolicy(task_t task
, uint64_t tid
);
529 int proc_apply_bgtask_selfpolicy(void);
530 int proc_apply_bgthread_selfpolicy(void);
531 int proc_apply_workq_bgthreadpolicy(thread_t
);
533 int proc_restore_bgtaskpolicy(task_t task
);
534 int proc_restore_bgthreadpolicy(task_t task
, uint64_t tid
);
535 int proc_restore_bgthread_selfpolicy(void);
536 int proc_restore_workq_bgthreadpolicy(thread_t
);
538 /* hw access routines */
539 int proc_apply_task_diskacc(task_t task
, int policy
);
540 int proc_apply_thread_diskacc(task_t task
, uint64_t tid
, int policy
);
541 int proc_apply_thread_selfdiskacc(int policy
);
542 int proc_get_task_disacc(task_t task
);
543 int proc_get_task_selfdiskacc(void);
544 int proc_get_thread_selfdiskacc(void);
545 int proc_denyinherit_policy(task_t task
);
546 int proc_denyselfset_policy(task_t task
);
548 int proc_get_task_selfgpuacc_deny(void);
549 int proc_apply_task_gpuacc(task_t task
, int prio
);
551 int proc_get_task_ruse_cpu(task_t task
, uint32_t * policyp
, uint32_t * percentagep
, uint64_t * intervalp
, uint64_t * deadlinep
);
552 int proc_set_task_ruse_cpu(task_t task
, uint32_t policy
, uint32_t percentage
, uint64_t interval
, uint64_t deadline
);
553 thread_t
task_findtid(task_t
, uint64_t);
555 #define PROC_POLICY_OSX_APPTYPE_NONE 0
556 #define PROC_POLICY_OSX_APPTYPE_TAL 1
557 #define PROC_POLICY_OSX_APPTYPE_WIDGET 2
558 #define PROC_POLICY_OSX_APPTYPE_DBCLIENT 2 /* Not a bug, just rename of widget */
559 #define PROC_POLICY_IOS_APPTYPE 3
560 #define PROC_POLICY_IOS_NONUITYPE 4
562 void proc_set_task_apptype(task_t
, int);
563 int proc_disable_task_apptype(task_t task
, int policy_subtype
);
564 int proc_enable_task_apptype(task_t task
, int policy_subtype
);
566 /* resource handle callback */
567 int task_action_cpuusage(task_t
);
569 /* BSD call back functions */
570 extern int proc_apply_resource_actions(void * p
, int type
, int action
);
571 extern int proc_restore_resource_actions(void * p
, int type
, int action
);
572 extern int task_restore_resource_actions(task_t task
, int type
);
574 extern void proc_apply_task_networkbg(void * bsd_info
);
575 extern void proc_restore_task_networkbg(void * bsd_info
);
576 extern void proc_set_task_networkbg(void * bsd_info
, int setbg
);
577 #endif /* XNU_KERNEL_PRIVATE */
579 #ifdef KERNEL_PRIVATE
581 extern void *get_bsdtask_info(task_t
);
582 extern void *get_bsdthreadtask_info(thread_t
);
583 extern vm_map_t
get_task_map(task_t
);
585 #endif /* KERNEL_PRIVATE */
587 extern task_t kernel_task
;
589 extern void task_deallocate(
592 extern void task_name_deallocate(
593 task_name_t task_name
);
596 #endif /* _KERN_TASK_H_ */