]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/bsd_kern.c
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 #include <mach/mach_types.h>
24 #include <kern/kern_types.h>
25 #include <kern/processor.h>
26 #include <kern/thread.h>
27 #include <kern/task.h>
29 #include <kern/lock.h>
31 #include <ipc/ipc_port.h>
32 #include <ipc/ipc_object.h>
33 #include <vm/vm_map.h>
35 #include <vm/vm_protos.h> /* last */
37 #undef thread_should_halt
38 #undef ipc_port_release
40 /* BSD KERN COMPONENT INTERFACE */
42 task_t bsd_init_task
= TASK_NULL
;
43 char init_task_failure_data
[1024];
44 extern unsigned int not_in_kdp
; /* Skip acquiring locks if we're in kdp */
46 thread_t
get_firstthread(task_t
);
47 int get_task_userstop(task_t
);
48 int get_thread_userstop(thread_t
);
49 boolean_t
thread_should_abort(thread_t
);
50 boolean_t
current_thread_aborted(void);
51 void task_act_iterate_wth_args(task_t
, void(*)(thread_t
, void *), void *);
52 void ipc_port_release(ipc_port_t
);
53 boolean_t
is_thread_active(thread_t
);
54 kern_return_t
get_signalact(task_t
, thread_t
*, int);
55 int get_vmsubmap_entries(vm_map_t
, vm_object_offset_t
, vm_object_offset_t
);
60 void *get_bsdtask_info(task_t t
)
68 void set_bsdtask_info(task_t t
,void * v
)
76 void *get_bsdthread_info(thread_t th
)
82 * XXX: wait for BSD to fix signal code
83 * Until then, we cannot block here. We know the task
84 * can't go away, so we make sure it is still active after
85 * retrieving the first thread for extra safety.
87 thread_t
get_firstthread(task_t task
)
89 thread_t thread
= (thread_t
)queue_first(&task
->threads
);
91 if (queue_end(&task
->threads
, (queue_entry_t
)thread
))
103 thread_t
*result_out
,
106 kern_return_t result
= KERN_SUCCESS
;
107 thread_t inc
, thread
= THREAD_NULL
;
114 return (KERN_FAILURE
);
117 for (inc
= (thread_t
)queue_first(&task
->threads
);
118 !queue_end(&task
->threads
, (queue_entry_t
)inc
); ) {
119 thread_mtx_lock(inc
);
121 (inc
->state
& (TH_ABORT
|TH_ABORT_SAFELY
)) != TH_ABORT
) {
125 thread_mtx_unlock(inc
);
127 inc
= (thread_t
)queue_next(&inc
->task_threads
);
131 *result_out
= thread
;
135 act_set_astbsd(thread
);
137 thread_mtx_unlock(thread
);
140 result
= KERN_FAILURE
;
154 kern_return_t result
= KERN_FAILURE
;
162 return (KERN_FAILURE
);
165 for (inc
= (thread_t
)queue_first(&task
->threads
);
166 !queue_end(&task
->threads
, (queue_entry_t
)inc
); ) {
168 thread_mtx_lock(inc
);
171 (inc
->state
& (TH_ABORT
|TH_ABORT_SAFELY
)) != TH_ABORT
) {
172 result
= KERN_SUCCESS
;
176 thread_mtx_unlock(inc
);
180 inc
= (thread_t
)queue_next(&inc
->task_threads
);
183 if (result
== KERN_SUCCESS
) {
185 act_set_astbsd(thread
);
187 thread_mtx_unlock(thread
);
196 * This is only safe to call from a thread executing in
197 * in the task's context or if the task is locked Otherwise,
198 * the map could be switched for the task (and freed) before
199 * we to return it here.
201 vm_map_t
get_task_map(task_t t
)
206 vm_map_t
get_task_map_reference(task_t t
)
219 vm_map_reference_swap(m
);
227 ipc_space_t
get_task_ipcspace(task_t t
)
229 return(t
->itk_space
);
232 int get_task_numacts(task_t t
)
234 return(t
->thread_count
);
237 /* does this machine need 64bit register set for signal handler */
238 int is_64signalregset(void)
240 task_t t
= current_task();
241 if(t
->taskFeatures
[0] & tf64BitData
)
248 * The old map reference is returned.
251 swap_task_map(task_t task
,vm_map_t map
)
253 thread_t thread
= current_thread();
256 if (task
!= thread
->task
)
257 panic("swap_task_map");
261 thread
->map
= task
->map
= map
;
269 pmap_t
get_task_pmap(task_t t
)
271 return(t
->map
->pmap
);
277 pmap_t
get_map_pmap(vm_map_t map
)
284 task_t
get_threadtask(thread_t th
)
293 boolean_t
is_thread_idle(thread_t th
)
295 return((th
->state
& TH_IDLE
) == TH_IDLE
);
301 boolean_t
is_thread_running(thread_t th
)
303 return((th
->state
& TH_RUN
) == TH_RUN
);
333 return(vm_map_min(map
));
343 return(vm_map_max(map
));
353 get_vmsubmap_entries(
355 vm_object_offset_t start
,
356 vm_object_offset_t end
)
358 int total_entries
= 0;
359 vm_map_entry_t entry
;
363 entry
= vm_map_first_entry(map
);
364 while((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< start
)) {
365 entry
= entry
->vme_next
;
368 while((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
369 if(entry
->is_sub_map
) {
371 get_vmsubmap_entries(entry
->object
.sub_map
,
374 (entry
->vme_end
- entry
->vme_start
));
378 entry
= entry
->vme_next
;
382 return(total_entries
);
389 int total_entries
= 0;
390 vm_map_entry_t entry
;
394 entry
= vm_map_first_entry(map
);
396 while(entry
!= vm_map_to_entry(map
)) {
397 if(entry
->is_sub_map
) {
399 get_vmsubmap_entries(entry
->object
.sub_map
,
402 (entry
->vme_end
- entry
->vme_start
));
406 entry
= entry
->vme_next
;
410 return(total_entries
);
423 return(task
->user_stop_count
);
433 return(th
->user_stop_count
);
443 return ((th
->state
& (TH_ABORT
|TH_ABORT_SAFELY
)) == TH_ABORT
);
447 * This routine is like thread_should_abort() above. It checks to
448 * see if the current thread is aborted. But unlike above, it also
449 * checks to see if thread is safely aborted. If so, it returns
450 * that fact, and clears the condition (safe aborts only should
451 * have a single effect, and a poll of the abort status
455 current_thread_aborted (
458 thread_t th
= current_thread();
461 if ((th
->state
& (TH_ABORT
|TH_ABORT_SAFELY
)) == TH_ABORT
&&
462 (th
->options
& TH_OPT_INTMASK
) != THREAD_UNINT
)
464 if (th
->state
& TH_ABORT_SAFELY
) {
467 if (th
->state
& TH_ABORT_SAFELY
)
468 th
->state
&= ~(TH_ABORT
|TH_ABORT_SAFELY
);
479 task_act_iterate_wth_args(
481 void (*func_callback
)(thread_t
, void *),
488 for (inc
= (thread_t
)queue_first(&task
->threads
);
489 !queue_end(&task
->threads
, (queue_entry_t
)inc
); ) {
490 (void) (*func_callback
)(inc
, func_arg
);
491 inc
= (thread_t
)queue_next(&inc
->task_threads
);
501 ipc_object_release(&(port
)->ip_object
);
516 reenable
= ml_set_interrupts_enabled(FALSE
);
517 ast_on_fast(AST_BSD
);
518 (void)ml_set_interrupts_enabled(reenable
);