]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/bsd_kern.c
338b5b284bf25e94a9f70f01734c79a1199ef365
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 #include <mach/mach_types.h>
32 #include <kern/kern_types.h>
33 #include <kern/processor.h>
34 #include <kern/thread.h>
35 #include <kern/task.h>
37 #include <kern/lock.h>
39 #include <ipc/ipc_port.h>
40 #include <ipc/ipc_object.h>
41 #include <vm/vm_map.h>
43 #include <vm/vm_protos.h> /* last */
45 #undef thread_should_halt
46 #undef ipc_port_release
48 /* BSD KERN COMPONENT INTERFACE */
50 task_t bsd_init_task
= TASK_NULL
;
51 char init_task_failure_data
[1024];
52 extern unsigned int not_in_kdp
; /* Skip acquiring locks if we're in kdp */
54 thread_t
get_firstthread(task_t
);
55 int get_task_userstop(task_t
);
56 int get_thread_userstop(thread_t
);
57 boolean_t
thread_should_abort(thread_t
);
58 boolean_t
current_thread_aborted(void);
59 void task_act_iterate_wth_args(task_t
, void(*)(thread_t
, void *), void *);
60 void ipc_port_release(ipc_port_t
);
61 boolean_t
is_thread_active(thread_t
);
62 kern_return_t
get_signalact(task_t
, thread_t
*, int);
63 int get_vmsubmap_entries(vm_map_t
, vm_object_offset_t
, vm_object_offset_t
);
68 void *get_bsdtask_info(task_t t
)
76 void set_bsdtask_info(task_t t
,void * v
)
84 void *get_bsdthread_info(thread_t th
)
90 * XXX: wait for BSD to fix signal code
91 * Until then, we cannot block here. We know the task
92 * can't go away, so we make sure it is still active after
93 * retrieving the first thread for extra safety.
95 thread_t
get_firstthread(task_t task
)
97 thread_t thread
= (thread_t
)queue_first(&task
->threads
);
99 if (queue_end(&task
->threads
, (queue_entry_t
)thread
))
100 thread
= THREAD_NULL
;
103 return (THREAD_NULL
);
111 thread_t
*result_out
,
114 kern_return_t result
= KERN_SUCCESS
;
115 thread_t inc
, thread
= THREAD_NULL
;
122 return (KERN_FAILURE
);
125 for (inc
= (thread_t
)queue_first(&task
->threads
);
126 !queue_end(&task
->threads
, (queue_entry_t
)inc
); ) {
127 thread_mtx_lock(inc
);
129 (inc
->state
& (TH_ABORT
|TH_ABORT_SAFELY
)) != TH_ABORT
) {
133 thread_mtx_unlock(inc
);
135 inc
= (thread_t
)queue_next(&inc
->task_threads
);
139 *result_out
= thread
;
143 act_set_astbsd(thread
);
145 thread_mtx_unlock(thread
);
148 result
= KERN_FAILURE
;
162 kern_return_t result
= KERN_FAILURE
;
170 return (KERN_FAILURE
);
173 for (inc
= (thread_t
)queue_first(&task
->threads
);
174 !queue_end(&task
->threads
, (queue_entry_t
)inc
); ) {
176 thread_mtx_lock(inc
);
179 (inc
->state
& (TH_ABORT
|TH_ABORT_SAFELY
)) != TH_ABORT
) {
180 result
= KERN_SUCCESS
;
184 thread_mtx_unlock(inc
);
188 inc
= (thread_t
)queue_next(&inc
->task_threads
);
191 if (result
== KERN_SUCCESS
) {
193 act_set_astbsd(thread
);
195 thread_mtx_unlock(thread
);
204 * This is only safe to call from a thread executing in
205 * in the task's context or if the task is locked Otherwise,
206 * the map could be switched for the task (and freed) before
207 * we to return it here.
209 vm_map_t
get_task_map(task_t t
)
214 vm_map_t
get_task_map_reference(task_t t
)
227 vm_map_reference_swap(m
);
235 ipc_space_t
get_task_ipcspace(task_t t
)
237 return(t
->itk_space
);
240 int get_task_numacts(task_t t
)
242 return(t
->thread_count
);
245 /* does this machine need 64bit register set for signal handler */
246 int is_64signalregset(void)
248 task_t t
= current_task();
249 if(t
->taskFeatures
[0] & tf64BitData
)
256 * The old map reference is returned.
259 swap_task_map(task_t task
,vm_map_t map
)
261 thread_t thread
= current_thread();
264 if (task
!= thread
->task
)
265 panic("swap_task_map");
269 thread
->map
= task
->map
= map
;
277 pmap_t
get_task_pmap(task_t t
)
279 return(t
->map
->pmap
);
285 pmap_t
get_map_pmap(vm_map_t map
)
292 task_t
get_threadtask(thread_t th
)
301 boolean_t
is_thread_idle(thread_t th
)
303 return((th
->state
& TH_IDLE
) == TH_IDLE
);
309 boolean_t
is_thread_running(thread_t th
)
311 return((th
->state
& TH_RUN
) == TH_RUN
);
341 return(vm_map_min(map
));
351 return(vm_map_max(map
));
361 get_vmsubmap_entries(
363 vm_object_offset_t start
,
364 vm_object_offset_t end
)
366 int total_entries
= 0;
367 vm_map_entry_t entry
;
371 entry
= vm_map_first_entry(map
);
372 while((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< start
)) {
373 entry
= entry
->vme_next
;
376 while((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
377 if(entry
->is_sub_map
) {
379 get_vmsubmap_entries(entry
->object
.sub_map
,
382 (entry
->vme_end
- entry
->vme_start
));
386 entry
= entry
->vme_next
;
390 return(total_entries
);
397 int total_entries
= 0;
398 vm_map_entry_t entry
;
402 entry
= vm_map_first_entry(map
);
404 while(entry
!= vm_map_to_entry(map
)) {
405 if(entry
->is_sub_map
) {
407 get_vmsubmap_entries(entry
->object
.sub_map
,
410 (entry
->vme_end
- entry
->vme_start
));
414 entry
= entry
->vme_next
;
418 return(total_entries
);
431 return(task
->user_stop_count
);
441 return(th
->user_stop_count
);
451 return ((th
->state
& (TH_ABORT
|TH_ABORT_SAFELY
)) == TH_ABORT
);
455 * This routine is like thread_should_abort() above. It checks to
456 * see if the current thread is aborted. But unlike above, it also
457 * checks to see if thread is safely aborted. If so, it returns
458 * that fact, and clears the condition (safe aborts only should
459 * have a single effect, and a poll of the abort status
463 current_thread_aborted (
466 thread_t th
= current_thread();
469 if ((th
->state
& (TH_ABORT
|TH_ABORT_SAFELY
)) == TH_ABORT
&&
470 (th
->options
& TH_OPT_INTMASK
) != THREAD_UNINT
)
472 if (th
->state
& TH_ABORT_SAFELY
) {
475 if (th
->state
& TH_ABORT_SAFELY
)
476 th
->state
&= ~(TH_ABORT
|TH_ABORT_SAFELY
);
487 task_act_iterate_wth_args(
489 void (*func_callback
)(thread_t
, void *),
496 for (inc
= (thread_t
)queue_first(&task
->threads
);
497 !queue_end(&task
->threads
, (queue_entry_t
)inc
); ) {
498 (void) (*func_callback
)(inc
, func_arg
);
499 inc
= (thread_t
)queue_next(&inc
->task_threads
);
509 ipc_object_release(&(port
)->ip_object
);
524 reenable
= ml_set_interrupts_enabled(FALSE
);
525 ast_on_fast(AST_BSD
);
526 (void)ml_set_interrupts_enabled(reenable
);