1 #include "private/pthread_support.h"
3 /* This probably needs more porting work to ppc64. */
5 # if defined(GC_DARWIN_THREADS)
7 /* From "Inside Mac OS X - Mach-O Runtime Architecture" published by Apple
9 "The space beneath the stack pointer, where a new stack frame would normally
10 be allocated, is called the red zone. This area as shown in Figure 3-2 may
11 be used for any purpose as long as a new stack frame does not need to be
14 Page 50: "If a leaf procedure's red zone usage would exceed 224 bytes, then
15 it must set up a stack frame just like routines that call other routines."
18 # define PPC_RED_ZONE_SIZE 224
19 #elif defined(__ppc64__)
20 # define PPC_RED_ZONE_SIZE 320
23 /* Try to work out the right way to access thread state structure members.
24 The structure has changed its definition in different Darwin versions. */
26 # define THREAD_STATE ppc_thread_state_t
27 # if defined (HAS_PPC_THREAD_STATE_R0)
28 # define THREAD_FLD(x) x
29 # elif defined (HAS_PPC_THREAD_STATE___R0)
30 # define THREAD_FLD(x) __ ## x
32 # error can not work out how to access fields of ppc_thread_state_t
34 #elif defined(__ppc64__)
35 # define THREAD_STATE ppc_thread_state64_t
36 # if defined (HAS_PPC_THREAD_STATE64_R0)
37 # define THREAD_FLD(x) x
38 # elif defined (HAS_PPC_THREAD_STATE64___R0)
39 # define THREAD_FLD(x) __ ## x
41 # error can not work out how to access fields of ppc_thread_state64_t
43 #elif defined(__i386__)
44 # define THREAD_STATE i386_thread_state_t
45 # if defined (HAS_I386_THREAD_STATE_EAX)
46 # define THREAD_FLD(x) x
47 # elif defined (HAS_I386_THREAD_STATE___EAX)
48 # define THREAD_FLD(x) __ ## x
50 # error can not work out how to access fields of i386_thread_state_t
53 # error unknown architecture
56 typedef struct StackFrame
{
57 unsigned long savedSP
;
58 unsigned long savedCR
;
59 unsigned long savedLR
;
60 unsigned long reserved
[2];
61 unsigned long savedRTOC
;
64 unsigned long FindTopOfStack(unsigned int stack_start
) {
67 if (stack_start
== 0) {
70 __asm__
volatile("lwz %0,0(r1)" : "=r" (frame
));
72 __asm__
volatile("ld %0,0(r1)" : "=r" (frame
));
76 frame
= (StackFrame
*)stack_start
;
80 /* GC_printf1("FindTopOfStack start at sp = %p\n", frame); */
83 if (frame
->savedSP
== 0) break;
84 /* if there are no more stack frames, stop */
86 frame
= (StackFrame
*)frame
->savedSP
;
88 /* we do these next two checks after going to the next frame
89 because the LR for the first stack frame in the loop
90 is not set up on purpose, so we shouldn't check it. */
91 if ((frame
->savedLR
& ~3) == 0) break; /* if the next LR is bogus, stop */
92 if ((~(frame
->savedLR
) & ~3) == 0) break; /* ditto */
96 /* GC_printf1("FindTopOfStack finish at sp = %p\n", frame); */
99 return (unsigned long)frame
;
102 #ifdef DARWIN_DONT_PARSE_STACK
103 void GC_push_all_stacks() {
110 mach_msg_type_number_t thread_state_count
= MACHINE_THREAD_STATE_COUNT
;
113 if (!GC_thr_initialized
) GC_thr_init();
115 for(i
=0;i
<THREAD_TABLE_SZ
;i
++) {
116 for(p
=GC_threads
[i
];p
!=0;p
=p
->next
) {
117 if(p
-> flags
& FINISHED
) continue;
118 if(pthread_equal(p
->id
,me
)) {
121 /* Get the thread state (registers, etc) */
122 r
= thread_get_state(
123 p
->stop_info
.mach_thread
,
124 MACHINE_THREAD_STATE
,
126 &thread_state_count
);
127 if(r
!= KERN_SUCCESS
) ABORT("thread_get_state failed");
129 lo
= (void*)(state
. THREAD_FLD (r1
) - PPC_RED_ZONE_SIZE
);
131 GC_push_one(state
. THREAD_FLD (r0
));
132 GC_push_one(state
. THREAD_FLD (r2
));
133 GC_push_one(state
. THREAD_FLD (r3
));
134 GC_push_one(state
. THREAD_FLD (r4
));
135 GC_push_one(state
. THREAD_FLD (r5
));
136 GC_push_one(state
. THREAD_FLD (r6
));
137 GC_push_one(state
. THREAD_FLD (r7
));
138 GC_push_one(state
. THREAD_FLD (r8
));
139 GC_push_one(state
. THREAD_FLD (r9
));
140 GC_push_one(state
. THREAD_FLD (r10
));
141 GC_push_one(state
. THREAD_FLD (r11
));
142 GC_push_one(state
. THREAD_FLD (r12
));
143 GC_push_one(state
. THREAD_FLD (r13
));
144 GC_push_one(state
. THREAD_FLD (r14
));
145 GC_push_one(state
. THREAD_FLD (r15
));
146 GC_push_one(state
. THREAD_FLD (r16
));
147 GC_push_one(state
. THREAD_FLD (r17
));
148 GC_push_one(state
. THREAD_FLD (r18
));
149 GC_push_one(state
. THREAD_FLD (r19
));
150 GC_push_one(state
. THREAD_FLD (r20
));
151 GC_push_one(state
. THREAD_FLD (r21
));
152 GC_push_one(state
. THREAD_FLD (r22
));
153 GC_push_one(state
. THREAD_FLD (r23
));
154 GC_push_one(state
. THREAD_FLD (r24
));
155 GC_push_one(state
. THREAD_FLD (r25
));
156 GC_push_one(state
. THREAD_FLD (r26
));
157 GC_push_one(state
. THREAD_FLD (r27
));
158 GC_push_one(state
. THREAD_FLD (r28
));
159 GC_push_one(state
. THREAD_FLD (r29
));
160 GC_push_one(state
. THREAD_FLD (r30
));
161 GC_push_one(state
. THREAD_FLD (r31
));
163 if(p
->flags
& MAIN_THREAD
)
168 GC_printf3("Darwin: Stack for thread 0x%lx = [%lx,%lx)\n",
169 (unsigned long) p
-> id
,
174 GC_push_all_stack(lo
,hi
);
175 } /* for(p=GC_threads[i]...) */
176 } /* for(i=0;i<THREAD_TABLE_SZ...) */
179 #else /* !DARWIN_DONT_PARSE_STACK; Use FindTopOfStack() */
181 void GC_push_all_stacks() {
186 thread_act_array_t act_list
= 0;
187 mach_msg_type_number_t listcount
= 0;
189 me
= mach_thread_self();
190 if (!GC_thr_initialized
) GC_thr_init();
192 r
= task_threads(current_task(), &act_list
, &listcount
);
193 if(r
!= KERN_SUCCESS
) ABORT("task_threads failed");
194 for(i
= 0; i
< listcount
; i
++) {
195 thread_act_t thread
= act_list
[i
];
198 hi
= (ptr_t
)FindTopOfStack(0);
200 # if defined(__ppc__) || defined(__ppc64__)
202 mach_msg_type_number_t outCount
= THREAD_STATE_MAX
;
203 r
= thread_get_state(thread
, MACHINE_THREAD_STATE
,
204 (natural_t
*)&info
, &outCount
);
205 if(r
!= KERN_SUCCESS
) ABORT("task_get_state failed");
207 lo
= (void*)(info
. THREAD_FLD (r1
) - PPC_RED_ZONE_SIZE
);
208 hi
= (ptr_t
)FindTopOfStack(info
. THREAD_FLD (r1
));
210 GC_push_one(info
. THREAD_FLD (r0
));
211 GC_push_one(info
. THREAD_FLD (r2
));
212 GC_push_one(info
. THREAD_FLD (r3
));
213 GC_push_one(info
. THREAD_FLD (r4
));
214 GC_push_one(info
. THREAD_FLD (r5
));
215 GC_push_one(info
. THREAD_FLD (r6
));
216 GC_push_one(info
. THREAD_FLD (r7
));
217 GC_push_one(info
. THREAD_FLD (r8
));
218 GC_push_one(info
. THREAD_FLD (r9
));
219 GC_push_one(info
. THREAD_FLD (r10
));
220 GC_push_one(info
. THREAD_FLD (r11
));
221 GC_push_one(info
. THREAD_FLD (r12
));
222 GC_push_one(info
. THREAD_FLD (r13
));
223 GC_push_one(info
. THREAD_FLD (r14
));
224 GC_push_one(info
. THREAD_FLD (r15
));
225 GC_push_one(info
. THREAD_FLD (r16
));
226 GC_push_one(info
. THREAD_FLD (r17
));
227 GC_push_one(info
. THREAD_FLD (r18
));
228 GC_push_one(info
. THREAD_FLD (r19
));
229 GC_push_one(info
. THREAD_FLD (r20
));
230 GC_push_one(info
. THREAD_FLD (r21
));
231 GC_push_one(info
. THREAD_FLD (r22
));
232 GC_push_one(info
. THREAD_FLD (r23
));
233 GC_push_one(info
. THREAD_FLD (r24
));
234 GC_push_one(info
. THREAD_FLD (r25
));
235 GC_push_one(info
. THREAD_FLD (r26
));
236 GC_push_one(info
. THREAD_FLD (r27
));
237 GC_push_one(info
. THREAD_FLD (r28
));
238 GC_push_one(info
. THREAD_FLD (r29
));
239 GC_push_one(info
. THREAD_FLD (r30
));
240 GC_push_one(info
. THREAD_FLD (r31
));
242 /* FIXME: Remove after testing: */
243 WARN("This is completely untested and likely will not work\n", 0);
245 mach_msg_type_number_t outCount
= THREAD_STATE_MAX
;
246 r
= thread_get_state(thread
, MACHINE_THREAD_STATE
,
247 (natural_t
*)&info
, &outCount
);
248 if(r
!= KERN_SUCCESS
) ABORT("task_get_state failed");
250 lo
= (void*)info
. THREAD_FLD (esp
);
251 hi
= (ptr_t
)FindTopOfStack(info
. THREAD_FLD (esp
));
253 GC_push_one(info
. THREAD_FLD (eax
));
254 GC_push_one(info
. THREAD_FLD (ebx
));
255 GC_push_one(info
. THREAD_FLD (ecx
));
256 GC_push_one(info
. THREAD_FLD (edx
));
257 GC_push_one(info
. THREAD_FLD (edi
));
258 GC_push_one(info
. THREAD_FLD (esi
));
259 /* GC_push_one(info . THREAD_FLD (ebp)); */
260 /* GC_push_one(info . THREAD_FLD (esp)); */
261 GC_push_one(info
. THREAD_FLD (ss
));
262 GC_push_one(info
. THREAD_FLD (eip
));
263 GC_push_one(info
. THREAD_FLD (cs
));
264 GC_push_one(info
. THREAD_FLD (ds
));
265 GC_push_one(info
. THREAD_FLD (es
));
266 GC_push_one(info
. THREAD_FLD (fs
));
267 GC_push_one(info
. THREAD_FLD (gs
));
268 # endif /* !POWERPC */
271 GC_printf3("Darwin: Stack for thread 0x%lx = [%lx,%lx)\n",
272 (unsigned long) thread
,
277 GC_push_all_stack(lo
, hi
);
278 } /* for(p=GC_threads[i]...) */
279 vm_deallocate(current_task(), (vm_address_t
)act_list
, sizeof(thread_t
) * listcount
);
281 #endif /* !DARWIN_DONT_PARSE_STACK */
283 static mach_port_t GC_mach_handler_thread
;
284 static int GC_use_mach_handler_thread
= 0;
286 static struct GC_mach_thread GC_mach_threads
[THREAD_TABLE_SZ
];
287 static int GC_mach_threads_count
;
289 void GC_stop_init() {
292 for (i
= 0; i
< THREAD_TABLE_SZ
; i
++) {
293 GC_mach_threads
[i
].thread
= 0;
294 GC_mach_threads
[i
].already_suspended
= 0;
296 GC_mach_threads_count
= 0;
299 /* returns true if there's a thread in act_list that wasn't in old_list */
300 int GC_suspend_thread_list(thread_act_array_t act_list
, int count
,
301 thread_act_array_t old_list
, int old_count
) {
302 mach_port_t my_thread
= mach_thread_self();
307 for(i
= 0; i
< count
; i
++) {
308 thread_act_t thread
= act_list
[i
];
310 GC_printf1("Attempting to suspend thread %p\n", thread
);
312 /* find the current thread in the old list */
314 for(j
= 0; j
< old_count
; j
++) {
315 thread_act_t old_thread
= old_list
[j
];
316 if (old_thread
== thread
) {
322 /* add it to the GC_mach_threads list */
323 GC_mach_threads
[GC_mach_threads_count
].thread
= thread
;
324 /* default is not suspended */
325 GC_mach_threads
[GC_mach_threads_count
].already_suspended
= 0;
329 if (thread
!= my_thread
&&
330 (!GC_use_mach_handler_thread
331 || (GC_use_mach_handler_thread
332 && GC_mach_handler_thread
!= thread
))) {
333 struct thread_basic_info info
;
334 mach_msg_type_number_t outCount
= THREAD_INFO_MAX
;
335 kern_return_t kern_result
= thread_info(thread
, THREAD_BASIC_INFO
,
336 (thread_info_t
)&info
, &outCount
);
337 if(kern_result
!= KERN_SUCCESS
) {
338 /* the thread may have quit since the thread_threads () call
339 * we mark already_suspended so it's not dealt with anymore later
342 GC_mach_threads
[GC_mach_threads_count
].already_suspended
= TRUE
;
343 GC_mach_threads_count
++;
348 GC_printf2("Thread state for 0x%lx = %d\n", thread
, info
.run_state
);
351 GC_mach_threads
[GC_mach_threads_count
].already_suspended
= info
.suspend_count
;
353 if (info
.suspend_count
) continue;
356 GC_printf1("Suspending 0x%lx\n", thread
);
358 /* Suspend the thread */
359 kern_result
= thread_suspend(thread
);
360 if(kern_result
!= KERN_SUCCESS
) {
361 /* the thread may have quit since the thread_threads () call
362 * we mark already_suspended so it's not dealt with anymore later
365 GC_mach_threads
[GC_mach_threads_count
].already_suspended
= TRUE
;
366 GC_mach_threads_count
++;
371 if (!found
) GC_mach_threads_count
++;
377 /* Caller holds allocation lock. */
382 mach_port_t my_thread
= mach_thread_self();
383 kern_return_t kern_result
;
384 thread_act_array_t act_list
, prev_list
;
385 mach_msg_type_number_t listcount
, prevcount
;
388 GC_printf1("Stopping the world from 0x%lx\n", mach_thread_self());
391 /* clear out the mach threads list table */
394 /* Make sure all free list construction has stopped before we start. */
395 /* No new construction can start, since free list construction is */
396 /* required to acquire and release the GC lock before it starts, */
397 /* and we have the lock. */
398 # ifdef PARALLEL_MARK
399 GC_acquire_mark_lock();
400 GC_ASSERT(GC_fl_builder_count
== 0);
401 /* We should have previously waited for it to become zero. */
402 # endif /* PARALLEL_MARK */
404 /* Loop stopping threads until you have gone over the whole list
405 twice without a new one appearing. thread_create() won't
406 return (and thus the thread stop) until the new thread
407 exists, so there is no window whereby you could stop a
408 thread, recognise it is stopped, but then have a new thread
409 it created before stopping show up later.
417 kern_result
= task_threads(current_task(), &act_list
, &listcount
);
418 result
= GC_suspend_thread_list(act_list
, listcount
,
419 prev_list
, prevcount
);
421 prev_list
= act_list
;
422 prevcount
= listcount
;
423 vm_deallocate(current_task(), (vm_address_t
)act_list
, sizeof(thread_t
) * listcount
);
429 extern void GC_mprotect_stop();
434 # ifdef PARALLEL_MARK
435 GC_release_mark_lock();
438 GC_printf1("World stopped from 0x%lx\n", my_thread
);
442 /* Caller holds allocation lock, and has held it continuously since */
443 /* the world stopped. */
444 void GC_start_world()
446 mach_port_t my_thread
= mach_thread_self();
449 kern_return_t kern_result
;
450 thread_act_array_t act_list
;
451 mach_msg_type_number_t listcount
;
452 struct thread_basic_info info
;
453 mach_msg_type_number_t outCount
= THREAD_INFO_MAX
;
456 GC_printf0("World starting\n");
461 extern void GC_mprotect_resume();
462 GC_mprotect_resume();
466 kern_result
= task_threads(current_task(), &act_list
, &listcount
);
467 for(i
= 0; i
< listcount
; i
++) {
468 thread_act_t thread
= act_list
[i
];
469 if (thread
!= my_thread
&&
470 (!GC_use_mach_handler_thread
||
471 (GC_use_mach_handler_thread
&& GC_mach_handler_thread
!= thread
))) {
472 for(j
= 0; j
< GC_mach_threads_count
; j
++) {
473 if (thread
== GC_mach_threads
[j
].thread
) {
474 if (GC_mach_threads
[j
].already_suspended
) {
476 GC_printf1("Not resuming already suspended thread %p\n", thread
);
480 kern_result
= thread_info(thread
, THREAD_BASIC_INFO
,
481 (thread_info_t
)&info
, &outCount
);
482 if(kern_result
!= KERN_SUCCESS
) ABORT("thread_info failed");
484 GC_printf2("Thread state for 0x%lx = %d\n", thread
,
486 GC_printf1("Resuming 0x%lx\n", thread
);
488 /* Resume the thread */
489 kern_result
= thread_resume(thread
);
490 if(kern_result
!= KERN_SUCCESS
) ABORT("thread_resume failed");
495 vm_deallocate(current_task(), (vm_address_t
)act_list
, sizeof(thread_t
) * listcount
);
497 GC_printf0("World started\n");
501 void GC_darwin_register_mach_handler_thread(mach_port_t thread
) {
502 GC_mach_handler_thread
= thread
;
503 GC_use_mach_handler_thread
= 1;