xnu-1486.2.11.tar.gz
[apple/xnu.git] / osfmk / kern / task.h
1 /*
2 * Copyright (c) 2000-2009 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 MACH_KERNEL_PRIVATE
99
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 <machine/task.h>
108
109 #include <kern/cpu_data.h>
110 #include <kern/queue.h>
111 #include <kern/exception.h>
112 #include <kern/lock.h>
113 #include <kern/thread.h>
114 #include <security/_label.h>
115 #include <ipc/ipc_labelh.h>
116
117 struct task {
118 /* Synchronization/destruction information */
119 decl_lck_mtx_data(,lock) /* Task's lock */
120 uint32_t ref_count; /* Number of references to me */
121 boolean_t active; /* Task has not been terminated */
122 boolean_t halting; /* Task is being halted */
123
124 /* Miscellaneous */
125 vm_map_t map; /* Address space description */
126 queue_chain_t tasks; /* global list of tasks */
127 void *user_data; /* Arbitrary data settable via IPC */
128
129 /* Threads in this task */
130 queue_head_t threads;
131
132 processor_set_t pset_hint;
133 struct affinity_space *affinity_space;
134
135 int thread_count;
136 uint32_t active_thread_count;
137 int suspend_count; /* Internal scheduling only */
138
139 /* User-visible scheduling information */
140 integer_t user_stop_count; /* outstanding stops */
141
142 task_role_t role;
143
144 integer_t priority; /* base priority for threads */
145 integer_t max_priority; /* maximum priority for threads */
146
147 /* Task security and audit tokens */
148 security_token_t sec_token;
149 audit_token_t audit_token;
150
151 /* Statistics */
152 uint64_t total_user_time; /* terminated threads only */
153 uint64_t total_system_time;
154
155 /* Virtual timers */
156 uint32_t vtimers;
157
158 /* IPC structures */
159 decl_lck_mtx_data(,itk_lock_data)
160 struct ipc_port *itk_self; /* not a right, doesn't hold ref */
161 struct ipc_port *itk_nself; /* not a right, doesn't hold ref */
162 struct ipc_port *itk_sself; /* a send right */
163 struct exception_action exc_actions[EXC_TYPES_COUNT];
164 /* a send right each valid element */
165 struct ipc_port *itk_host; /* a send right */
166 struct ipc_port *itk_bootstrap; /* a send right */
167 struct ipc_port *itk_seatbelt; /* a send right */
168 struct ipc_port *itk_gssd; /* yet another send right */
169 struct ipc_port *itk_task_access; /* and another send right */
170 struct ipc_port *itk_registered[TASK_PORT_REGISTER_MAX];
171 /* all send rights */
172
173 struct ipc_space *itk_space;
174
175 /* Synchronizer ownership information */
176 queue_head_t semaphore_list; /* list of owned semaphores */
177 queue_head_t lock_set_list; /* list of owned lock sets */
178 int semaphores_owned; /* number of semaphores owned */
179 int lock_sets_owned; /* number of lock sets owned */
180
181 /* Ledgers */
182 struct ipc_port *wired_ledger_port;
183 struct ipc_port *paged_ledger_port;
184 unsigned int priv_flags; /* privilege resource flags */
185 #define VM_BACKING_STORE_PRIV 0x1
186
187 MACHINE_TASK
188
189 integer_t faults; /* faults counter */
190 integer_t pageins; /* pageins counter */
191 integer_t cow_faults; /* copy on write fault counter */
192 integer_t messages_sent; /* messages sent counter */
193 integer_t messages_received; /* messages received counter */
194 integer_t syscalls_mach; /* mach system call counter */
195 integer_t syscalls_unix; /* unix system call counter */
196 uint32_t c_switch; /* total context switches */
197 uint32_t p_switch; /* total processor switches */
198 uint32_t ps_switch; /* total pset switches */
199 #ifdef MACH_BSD
200 void *bsd_info;
201 #endif
202 struct vm_shared_region *shared_region;
203 uint32_t taskFeatures[2]; /* Special feature for this task */
204 #define tf64BitAddr 0x80000000 /* Task has 64-bit addressing */
205 #define tf64BitData 0x40000000 /* Task has 64-bit data registers */
206 #define task_has_64BitAddr(task) \
207 (((task)->taskFeatures[0] & tf64BitAddr) != 0)
208 #define task_set_64BitAddr(task) \
209 ((task)->taskFeatures[0] |= tf64BitAddr)
210 #define task_clear_64BitAddr(task) \
211 ((task)->taskFeatures[0] &= ~tf64BitAddr)
212
213 mach_vm_address_t all_image_info_addr; /* dyld __all_image_info */
214 mach_vm_size_t all_image_info_size; /* section location and size */
215 #if CONFIG_MACF_MACH
216 ipc_labelh_t label;
217 #endif
218
219 #if CONFIG_COUNTERS
220 #define TASK_PMC_FLAG 0x1 /* Bit in "t_chud" signifying PMC interest */
221 uint32_t t_chud; /* CHUD flags, used for Shark */
222 #endif
223
224 };
225
226 #define task_lock(task) lck_mtx_lock(&(task)->lock)
227 #define task_lock_try(task) lck_mtx_try_lock(&(task)->lock)
228 #define task_unlock(task) lck_mtx_unlock(&(task)->lock)
229
230 #if CONFIG_MACF_MACH
231 #define maclabel label->lh_label
232
233 #define tasklabel_lock(task) lh_lock((task)->label)
234 #define tasklabel_unlock(task) lh_unlock((task)->label)
235
236 extern void tasklabel_lock2(task_t a, task_t b);
237 extern void tasklabel_unlock2(task_t a, task_t b);
238 #endif /* MAC_MACH */
239
240 #define itk_lock_init(task) lck_mtx_init(&(task)->itk_lock_data, &ipc_lck_grp, &ipc_lck_attr)
241 #define itk_lock_destroy(task) lck_mtx_destroy(&(task)->itk_lock_data, &ipc_lck_grp)
242 #define itk_lock(task) lck_mtx_lock(&(task)->itk_lock_data)
243 #define itk_unlock(task) lck_mtx_unlock(&(task)->itk_lock_data)
244
245 #define task_reference_internal(task) \
246 (void)hw_atomic_add(&(task)->ref_count, 1)
247
248 #define task_deallocate_internal(task) \
249 hw_atomic_sub(&(task)->ref_count, 1)
250
251 #define task_reference(task) \
252 MACRO_BEGIN \
253 if ((task) != TASK_NULL) \
254 task_reference_internal(task); \
255 MACRO_END
256
257 extern kern_return_t kernel_task_create(
258 task_t task,
259 vm_offset_t map_base,
260 vm_size_t map_size,
261 task_t *child);
262
263 /* Initialize task module */
264 extern void task_init(void) __attribute__((section("__TEXT, initcode")));
265
266 #define current_task_fast() (current_thread()->task)
267 #define current_task() current_task_fast()
268
269 extern lck_attr_t task_lck_attr;
270 extern lck_grp_t task_lck_grp;
271
272 #else /* MACH_KERNEL_PRIVATE */
273
274 __BEGIN_DECLS
275
276 extern task_t current_task(void);
277
278 extern void task_reference(task_t task);
279
280 __END_DECLS
281
282 #endif /* MACH_KERNEL_PRIVATE */
283
284 __BEGIN_DECLS
285
286 #ifdef XNU_KERNEL_PRIVATE
287
288 /* Hold all threads in a task */
289 extern kern_return_t task_hold(
290 task_t task);
291
292 /* Release hold on all threads in a task */
293 extern kern_return_t task_release(
294 task_t task);
295
296 /* Halt all other threads in the current task */
297 extern kern_return_t task_start_halt(
298 task_t task);
299
300 /* Wait for other threads to halt and free halting task resources */
301 extern void task_complete_halt(
302 task_t task);
303
304 extern kern_return_t task_terminate_internal(
305 task_t task);
306
307 extern kern_return_t task_create_internal(
308 task_t parent_task,
309 boolean_t inherit_memory,
310 boolean_t is_64bit,
311 task_t *child_task); /* OUT */
312
313 extern kern_return_t task_importance(
314 task_t task,
315 integer_t importance);
316
317 extern void task_vtimer_set(
318 task_t task,
319 integer_t which);
320
321 extern void task_vtimer_clear(
322 task_t task,
323 integer_t which);
324
325 extern void task_vtimer_update(
326 task_t task,
327 integer_t which,
328 uint32_t *microsecs);
329
330 #define TASK_VTIMER_USER 0x01
331 #define TASK_VTIMER_PROF 0x02
332 #define TASK_VTIMER_RLIM 0x04
333
334 extern void task_set_64bit(
335 task_t task,
336 boolean_t is64bit);
337
338 extern void task_backing_store_privileged(
339 task_t task);
340
341 extern void task_set_dyld_info(
342 task_t task,
343 mach_vm_address_t addr,
344 mach_vm_size_t size);
345
346 /* Get number of activations in a task */
347 extern int get_task_numacts(
348 task_t task);
349
350 extern int get_task_numactivethreads(task_t task);
351
352 /* JMM - should just be temporary (implementation in bsd_kern still) */
353 extern void set_bsdtask_info(task_t,void *);
354 extern vm_map_t get_task_map_reference(task_t);
355 extern vm_map_t swap_task_map(task_t, thread_t, vm_map_t);
356 extern pmap_t get_task_pmap(task_t);
357 extern uint64_t get_task_resident_size(task_t);
358
359 extern boolean_t is_kerneltask(task_t task);
360
361 extern kern_return_t check_actforsig(task_t task, thread_t thread, int setast);
362
363 extern kern_return_t machine_task_get_state(
364 task_t task,
365 int flavor,
366 thread_state_t state,
367 mach_msg_type_number_t *state_count);
368
369 extern kern_return_t machine_task_set_state(
370 task_t task,
371 int flavor,
372 thread_state_t state,
373 mach_msg_type_number_t state_count);
374
375
376 #endif /* XNU_KERNEL_PRIVATE */
377
378 #ifdef KERNEL_PRIVATE
379
380 extern void *get_bsdtask_info(task_t);
381 extern void *get_bsdthreadtask_info(thread_t);
382 extern vm_map_t get_task_map(task_t);
383
384 #endif /* KERNEL_PRIVATE */
385
386 extern task_t kernel_task;
387
388 extern void task_deallocate(
389 task_t task);
390
391 extern void task_name_deallocate(
392 task_name_t task_name);
393 __END_DECLS
394
395 #endif /* _KERN_TASK_H_ */