2 * Copyright (c) 2000 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>
23 #include <kern/queue.h>
25 #include <kern/thread.h>
26 #include <kern/thread_act.h>
27 #include <kern/task.h>
29 #include <kern/lock.h>
30 #include <vm/vm_map.h>
32 #include <ipc/ipc_port.h>
33 #include <ipc/ipc_object.h>
35 #undef thread_should_halt
36 #undef ipc_port_release
38 decl_simple_lock_data(extern,reaper_lock
)
39 extern queue_head_t reaper_queue
;
41 /* BSD KERN COMPONENT INTERFACE */
43 task_t bsd_init_task
= TASK_NULL
;
44 char init_task_failure_data
[1024];
46 thread_act_t
get_firstthread(task_t
);
47 vm_map_t
get_task_map(task_t
);
48 ipc_space_t
get_task_ipcspace(task_t
);
49 boolean_t
is_kerneltask(task_t
);
50 boolean_t
is_thread_idle(thread_t
);
51 boolean_t
is_thread_running(thread_act_t
);
52 thread_shuttle_t
getshuttle_thread( thread_act_t
);
53 thread_act_t
getact_thread( thread_shuttle_t
);
54 vm_offset_t
get_map_min( vm_map_t
);
55 vm_offset_t
get_map_max( vm_map_t
);
56 int get_task_userstop(task_t
);
57 int get_thread_userstop(thread_act_t
);
58 boolean_t
thread_should_abort(thread_shuttle_t
);
59 boolean_t
current_thread_aborted(void);
60 void task_act_iterate_wth_args(task_t
, void(*)(thread_act_t
, void *), void *);
61 void ipc_port_release(ipc_port_t
);
62 boolean_t
is_thread_active(thread_t
);
63 kern_return_t
get_thread_waitresult(thread_t
);
64 vm_size_t
get_vmmap_size(vm_map_t
);
65 int get_vmmap_entries(vm_map_t
);
66 int get_task_numacts(task_t
);
67 thread_act_t
get_firstthread(task_t task
);
68 kern_return_t
get_signalact(task_t
, thread_act_t
*, thread_t
*, int);
74 void *get_bsdtask_info(task_t t
)
82 void set_bsdtask_info(task_t t
,void * v
)
90 void *get_bsdthread_info(thread_act_t th
)
96 * XXX: wait for BSD to fix signal code
97 * Until then, we cannot block here. We know the task
98 * can't go away, so we make sure it is still active after
99 * retrieving the first thread for extra safety.
101 thread_act_t
get_firstthread(task_t task
)
103 thread_act_t thr_act
;
105 thr_act
= (thread_act_t
)queue_first(&task
->thr_acts
);
106 if (thr_act
== (thread_act_t
)&task
->thr_acts
)
107 thr_act
= THR_ACT_NULL
;
109 return(THR_ACT_NULL
);
113 kern_return_t
get_signalact(task_t task
,thread_act_t
* thact
, thread_t
* thshut
, int setast
)
118 thread_act_t thr_act
;
124 return(KERN_FAILURE
);
127 thr_act
= THR_ACT_NULL
;
128 for (inc
= (thread_act_t
)queue_first(&task
->thr_acts
);
129 inc
!= (thread_act_t
)&task
->thr_acts
;
131 th
= act_lock_thread(inc
);
133 ((th
->state
& (TH_ABORT
|TH_ABORT_SAFELY
)) != TH_ABORT
)) {
137 act_unlock_thread(inc
);
138 ninc
= (thread_act_t
)queue_next(&inc
->thr_acts
);
145 *thshut
= thr_act
? thr_act
->thread
: THREAD_NULL
;
148 act_set_astbsd(thr_act
);
150 act_unlock_thread(thr_act
);
155 return(KERN_SUCCESS
);
157 return(KERN_FAILURE
);
161 kern_return_t
check_actforsig(task_t task
, thread_act_t thact
, thread_t
* thshut
, int setast
)
166 thread_act_t thr_act
;
173 return(KERN_FAILURE
);
176 thr_act
= THR_ACT_NULL
;
177 for (inc
= (thread_act_t
)queue_first(&task
->thr_acts
);
178 inc
!= (thread_act_t
)&task
->thr_acts
;
182 ninc
= (thread_act_t
)queue_next(&inc
->thr_acts
);
185 th
= act_lock_thread(inc
);
187 ((th
->state
& (TH_ABORT
|TH_ABORT_SAFELY
)) != TH_ABORT
)) {
192 act_unlock_thread(inc
);
193 /* ninc = (thread_act_t)queue_next(&inc->thr_acts); */
199 *thshut
= thr_act
? thr_act
->thread
: THREAD_NULL
;
201 act_set_astbsd(thr_act
);
203 act_unlock_thread(thr_act
);
208 return(KERN_SUCCESS
);
210 return(KERN_FAILURE
);
216 vm_map_t
get_task_map(task_t t
)
224 ipc_space_t
get_task_ipcspace(task_t t
)
226 return(t
->itk_space
);
229 int get_task_numacts(task_t t
)
231 return(t
->thr_act_count
);
235 * Reset the current task's map by taking a reference
236 * on the new map. The old map reference is returned.
239 swap_task_map(task_t task
,vm_map_t map
)
243 vm_map_reference(map
);
252 * Reset the current act map.
253 * The caller donates us a reference to the new map
254 * and we donote our reference to the old map to him.
257 swap_act_map(thread_act_t thr_act
,vm_map_t map
)
262 old_map
= thr_act
->map
;
271 pmap_t
get_task_pmap(task_t t
)
273 return(t
->map
->pmap
);
279 pmap_t
get_map_pmap(vm_map_t map
)
286 task_t
get_threadtask(thread_act_t th
)
295 boolean_t
is_thread_idle(thread_t th
)
297 return((th
->state
& TH_IDLE
) == TH_IDLE
);
303 boolean_t
is_thread_running(thread_act_t thact
)
305 thread_t th
= thact
->thread
;
306 return((th
->state
& TH_RUN
) == TH_RUN
);
342 return(vm_map_min(map
));
352 return(vm_map_max(map
));
362 get_vmsubmap_entries(
364 vm_object_offset_t start
,
365 vm_object_offset_t end
)
367 int total_entries
= 0;
368 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
;
389 return(total_entries
);
396 int total_entries
= 0;
397 vm_map_entry_t entry
;
400 entry
= vm_map_first_entry(map
);
402 while(entry
!= vm_map_to_entry(map
)) {
403 if(entry
->is_sub_map
) {
405 get_vmsubmap_entries(entry
->object
.sub_map
,
408 (entry
->vme_end
- entry
->vme_start
));
412 entry
= entry
->vme_next
;
415 return(total_entries
);
428 return(task
->user_stop_count
);
438 return(th
->user_stop_count
);
448 return(!th
->top_act
|| !th
->top_act
->active
||
449 (th
->state
& (TH_ABORT
|TH_ABORT_SAFELY
)) == TH_ABORT
);
453 * This routine is like thread_should_abort() above. It checks to
454 * see if the current thread is aborted. But unlike above, it also
455 * checks to see if thread is safely aborted. If so, it returns
456 * that fact, and clears the condition (safe aborts only should
457 * have a single effect, and a poll of the abort status
461 current_thread_aborted (
464 thread_t th
= current_thread();
468 ((th
->state
& (TH_ABORT
|TH_ABORT_SAFELY
)) == TH_ABORT
&&
469 th
->interrupt_level
!= THREAD_UNINT
))
471 if (th
->state
& TH_ABORT_SAFELY
) {
474 if (th
->state
& TH_ABORT_SAFELY
)
475 th
->state
&= ~(TH_ABORT
|TH_ABORT_SAFELY
);
486 task_act_iterate_wth_args(
488 void (*func_callback
)(thread_act_t
, void *),
491 thread_act_t inc
, ninc
;
494 for (inc
= (thread_act_t
)queue_first(&task
->thr_acts
);
495 inc
!= (thread_act_t
)&task
->thr_acts
;
497 ninc
= (thread_act_t
)queue_next(&inc
->thr_acts
);
498 (void) (*func_callback
)(inc
, func_arg
);
507 ipc_object_release(&(port
)->ip_object
);
518 get_thread_waitresult(
521 return(th
->wait_result
);
529 reenable
= ml_set_interrupts_enabled(FALSE
);
530 ast_on_fast(AST_BSD
);
531 (void)ml_set_interrupts_enabled(reenable
);