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 #undef thread_ast_clear
40 decl_simple_lock_data(extern,reaper_lock
)
41 extern queue_head_t reaper_queue
;
43 /* BSD KERN COMPONENT INTERFACE */
45 vm_address_t bsd_init_task
= 0;
46 char init_task_failure_data
[1024];
48 thread_act_t
get_firstthread(task_t
);
49 vm_map_t
get_task_map(task_t
);
50 ipc_space_t
get_task_ipcspace(task_t
);
51 boolean_t
is_kerneltask(task_t
);
52 boolean_t
is_thread_idle(thread_t
);
53 boolean_t
is_thread_running(thread_t
);
54 thread_shuttle_t
getshuttle_thread( thread_act_t
);
55 thread_act_t
getact_thread( thread_shuttle_t
);
56 vm_offset_t
get_map_min( vm_map_t
);
57 vm_offset_t
get_map_max( vm_map_t
);
58 int get_task_userstop(task_t
);
59 int get_thread_userstop(thread_act_t
);
60 int inc_task_userstop(task_t
);
61 boolean_t
thread_should_abort(thread_shuttle_t
);
62 boolean_t
current_thread_aborted(void);
63 void task_act_iterate_wth_args(task_t
, void(*)(thread_act_t
, void *), void *);
64 void ipc_port_release(ipc_port_t
);
65 void thread_ast_set(thread_act_t
, ast_t
);
66 void thread_ast_clear(thread_act_t
, ast_t
);
67 boolean_t
is_thread_active(thread_t
);
68 event_t
get_thread_waitevent(thread_t
);
69 kern_return_t
get_thread_waitresult(thread_t
);
70 vm_size_t
get_vmmap_size(vm_map_t
);
71 int get_vmmap_entries(vm_map_t
);
72 int get_task_numacts(task_t
);
73 thread_act_t
get_firstthread(task_t task
);
74 kern_return_t
get_signalact(task_t
, thread_act_t
*, thread_t
*, int);
76 kern_return_t
bsd_refvm_object(vm_object_t object
);
82 void *get_bsdtask_info(task_t t
)
90 void set_bsdtask_info(task_t t
,void * v
)
98 void *get_bsdthread_info(thread_act_t th
)
104 * XXX: wait for BSD to fix signal code
105 * Until then, we cannot block here. We know the task
106 * can't go away, so we make sure it is still active after
107 * retrieving the first thread for extra safety.
109 thread_act_t
get_firstthread(task_t task
)
111 thread_act_t thr_act
;
113 thr_act
= (thread_act_t
)queue_first(&task
->thr_acts
);
114 if (thr_act
== (thread_act_t
)&task
->thr_acts
)
115 thr_act
= THR_ACT_NULL
;
117 return(THR_ACT_NULL
);
121 kern_return_t
get_signalact(task_t task
,thread_act_t
* thact
, thread_t
* thshut
, int setast
)
126 thread_act_t thr_act
;
132 return(KERN_FAILURE
);
135 thr_act
= THR_ACT_NULL
;
136 for (inc
= (thread_act_t
)queue_first(&task
->thr_acts
);
137 inc
!= (thread_act_t
)&task
->thr_acts
;
139 th
= act_lock_thread(inc
);
140 if ((inc
->active
) && ((th
->state
& TH_ABORT
) != TH_ABORT
)) {
144 act_unlock_thread(inc
);
145 ninc
= (thread_act_t
)queue_next(&inc
->thr_acts
);
152 *thshut
= thr_act
? thr_act
->thread
: THREAD_NULL
;
155 thread_ast_set(thr_act
, AST_BSD
);
156 if (current_act() == thr_act
)
159 act_unlock_thread(thr_act
);
164 return(KERN_SUCCESS
);
166 return(KERN_FAILURE
);
172 vm_map_t
get_task_map(task_t t
)
180 ipc_space_t
get_task_ipcspace(task_t t
)
182 return(t
->itk_space
);
185 int get_task_numacts(task_t t
)
187 return(t
->thr_act_count
);
191 * Reset the current task's map by taking a reference
192 * on the new map. The old map reference is returned.
195 swap_task_map(task_t task
,vm_map_t map
)
199 vm_map_reference(map
);
208 * Reset the current act map.
209 * The caller donates us a reference to the new map
210 * and we donote our reference to the old map to him.
213 swap_act_map(thread_act_t thr_act
,vm_map_t map
)
218 old_map
= thr_act
->map
;
227 pmap_t
get_task_pmap(task_t t
)
229 return(t
->map
->pmap
);
235 pmap_t
get_map_pmap(vm_map_t map
)
242 task_t
get_threadtask(thread_act_t th
)
251 boolean_t
is_thread_idle(thread_t th
)
253 return((th
->state
& TH_IDLE
) == TH_IDLE
);
259 boolean_t
is_thread_running(thread_t th
)
261 return((th
->state
& TH_RUN
) == TH_RUN
);
297 return(vm_map_min(map
));
307 return(vm_map_max(map
));
317 get_vmsubmap_entries(
319 vm_object_offset_t start
,
320 vm_object_offset_t end
)
322 int total_entries
= 0;
323 vm_map_entry_t entry
;
326 entry
= vm_map_first_entry(map
);
327 while((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< start
)) {
328 entry
= entry
->vme_next
;
331 while((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
332 if(entry
->is_sub_map
) {
334 get_vmsubmap_entries(entry
->object
.sub_map
,
337 (entry
->vme_end
- entry
->vme_start
));
341 entry
= entry
->vme_next
;
344 return(total_entries
);
351 int total_entries
= 0;
352 vm_map_entry_t entry
;
355 entry
= vm_map_first_entry(map
);
357 while(entry
!= vm_map_to_entry(map
)) {
358 if(entry
->is_sub_map
) {
360 get_vmsubmap_entries(entry
->object
.sub_map
,
363 (entry
->vme_end
- entry
->vme_start
));
367 entry
= entry
->vme_next
;
370 return(total_entries
);
383 return(task
->user_stop_count
);
393 return(th
->user_stop_count
);
404 i
= task
->user_stop_count
;
405 task
->user_stop_count
++;
417 return( (!th
->top_act
|| !th
->top_act
->active
||
418 th
->state
& TH_ABORT
));
425 current_thread_aborted (
428 thread_t th
= current_thread();
430 return(!th
->top_act
||
431 ((th
->state
& TH_ABORT
) && (th
->interruptible
)));
438 task_act_iterate_wth_args(
440 void (*func_callback
)(thread_act_t
, void *),
443 thread_act_t inc
, ninc
;
446 for (inc
= (thread_act_t
)queue_first(&task
->thr_acts
);
447 inc
!= (thread_act_t
)&task
->thr_acts
;
449 ninc
= (thread_act_t
)queue_next(&inc
->thr_acts
);
450 (void) (*func_callback
)(inc
, func_arg
);
459 ipc_object_release(&(port
)->ip_object
);
474 act
->ast
&= ~(reason
);
485 get_thread_waitevent(
488 return(th
->wait_event
);
492 get_thread_waitresult(
495 return(th
->wait_result
);
499 bsd_refvm_object(vm_object_t object
)
501 vm_object_reference(object
);
502 return(KERN_SUCCESS
);