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
52 #elif defined(__x86_64__)
53 # define THREAD_STATE i386_thread_state_t
54 # if defined (HAS_I386_THREAD_STATE_EAX)
55 # define THREAD_FLD(x) x
56 # elif defined (HAS_I386_THREAD_STATE___EAX)
57 # define THREAD_FLD(x) __ ## x
59 # error can not work out how to access fields of i386_thread_state_t
62 # error unknown architecture
65 typedef struct StackFrame
{
66 unsigned long savedSP
;
67 unsigned long savedCR
;
68 unsigned long savedLR
;
69 unsigned long reserved
[2];
70 unsigned long savedRTOC
;
73 unsigned long FindTopOfStack(unsigned long stack_start
) {
76 if (stack_start
== 0) {
79 __asm__
volatile("lwz %0,0(r1)" : "=r" (frame
));
81 __asm__
volatile("ld %0,0(r1)" : "=r" (frame
));
85 frame
= (StackFrame
*)stack_start
;
89 /* GC_printf1("FindTopOfStack start at sp = %p\n", frame); */
92 if (frame
->savedSP
== 0) break;
93 /* if there are no more stack frames, stop */
95 frame
= (StackFrame
*)frame
->savedSP
;
97 /* we do these next two checks after going to the next frame
98 because the LR for the first stack frame in the loop
99 is not set up on purpose, so we shouldn't check it. */
100 if ((frame
->savedLR
& ~3) == 0) break; /* if the next LR is bogus, stop */
101 if ((~(frame
->savedLR
) & ~3) == 0) break; /* ditto */
104 # ifdef DEBUG_THREADS
105 /* GC_printf1("FindTopOfStack finish at sp = %p\n", frame); */
108 return (unsigned long)frame
;
111 #ifdef DARWIN_DONT_PARSE_STACK
112 void GC_push_all_stacks() {
119 mach_msg_type_number_t thread_state_count
= MACHINE_THREAD_STATE_COUNT
;
122 if (!GC_thr_initialized
) GC_thr_init();
124 for(i
=0;i
<THREAD_TABLE_SZ
;i
++) {
125 for(p
=GC_threads
[i
];p
!=0;p
=p
->next
) {
126 if(p
-> flags
& FINISHED
) continue;
127 if(pthread_equal(p
->id
,me
)) {
130 /* Get the thread state (registers, etc) */
131 r
= thread_get_state(
132 p
->stop_info
.mach_thread
,
133 MACHINE_THREAD_STATE
,
135 &thread_state_count
);
136 if(r
!= KERN_SUCCESS
) ABORT("thread_get_state failed");
139 lo
= (void*)state
. THREAD_FLD (esp
);
141 GC_push_one(state
. THREAD_FLD (eax
));
142 GC_push_one(state
. THREAD_FLD (ebx
));
143 GC_push_one(state
. THREAD_FLD (ecx
));
144 GC_push_one(state
. THREAD_FLD (edx
));
145 GC_push_one(state
. THREAD_FLD (edi
));
146 GC_push_one(state
. THREAD_FLD (esi
));
147 GC_push_one(state
. THREAD_FLD (ebp
));
148 #elif defined(POWERPC)
149 lo
= (void*)(state
. THREAD_FLD (r1
) - PPC_RED_ZONE_SIZE
);
151 GC_push_one(state
. THREAD_FLD (r0
));
152 GC_push_one(state
. THREAD_FLD (r2
));
153 GC_push_one(state
. THREAD_FLD (r3
));
154 GC_push_one(state
. THREAD_FLD (r4
));
155 GC_push_one(state
. THREAD_FLD (r5
));
156 GC_push_one(state
. THREAD_FLD (r6
));
157 GC_push_one(state
. THREAD_FLD (r7
));
158 GC_push_one(state
. THREAD_FLD (r8
));
159 GC_push_one(state
. THREAD_FLD (r9
));
160 GC_push_one(state
. THREAD_FLD (r10
));
161 GC_push_one(state
. THREAD_FLD (r11
));
162 GC_push_one(state
. THREAD_FLD (r12
));
163 GC_push_one(state
. THREAD_FLD (r13
));
164 GC_push_one(state
. THREAD_FLD (r14
));
165 GC_push_one(state
. THREAD_FLD (r15
));
166 GC_push_one(state
. THREAD_FLD (r16
));
167 GC_push_one(state
. THREAD_FLD (r17
));
168 GC_push_one(state
. THREAD_FLD (r18
));
169 GC_push_one(state
. THREAD_FLD (r19
));
170 GC_push_one(state
. THREAD_FLD (r20
));
171 GC_push_one(state
. THREAD_FLD (r21
));
172 GC_push_one(state
. THREAD_FLD (r22
));
173 GC_push_one(state
. THREAD_FLD (r23
));
174 GC_push_one(state
. THREAD_FLD (r24
));
175 GC_push_one(state
. THREAD_FLD (r25
));
176 GC_push_one(state
. THREAD_FLD (r26
));
177 GC_push_one(state
. THREAD_FLD (r27
));
178 GC_push_one(state
. THREAD_FLD (r28
));
179 GC_push_one(state
. THREAD_FLD (r29
));
180 GC_push_one(state
. THREAD_FLD (r30
));
181 GC_push_one(state
. THREAD_FLD (r31
));
183 # error FIXME for non-x86 || ppc architectures
186 if(p
->flags
& MAIN_THREAD
)
191 GC_printf3("Darwin: Stack for thread 0x%lx = [%lx,%lx)\n",
192 (unsigned long) p
-> id
,
197 GC_push_all_stack(lo
,hi
);
198 } /* for(p=GC_threads[i]...) */
199 } /* for(i=0;i<THREAD_TABLE_SZ...) */
202 #else /* !DARWIN_DONT_PARSE_STACK; Use FindTopOfStack() */
204 void GC_push_all_stacks() {
209 thread_act_array_t act_list
= 0;
210 mach_msg_type_number_t listcount
= 0;
212 me
= mach_thread_self();
213 if (!GC_thr_initialized
) GC_thr_init();
215 r
= task_threads(current_task(), &act_list
, &listcount
);
216 if(r
!= KERN_SUCCESS
) ABORT("task_threads failed");
217 for(i
= 0; i
< listcount
; i
++) {
218 thread_act_t thread
= act_list
[i
];
221 hi
= (ptr_t
)FindTopOfStack(0);
223 # if defined(__ppc__) || defined(__ppc64__)
225 mach_msg_type_number_t outCount
= THREAD_STATE_MAX
;
226 r
= thread_get_state(thread
, MACHINE_THREAD_STATE
,
227 (natural_t
*)&info
, &outCount
);
228 if(r
!= KERN_SUCCESS
) ABORT("task_get_state failed");
230 lo
= (void*)(info
. THREAD_FLD (r1
) - PPC_RED_ZONE_SIZE
);
231 hi
= (ptr_t
)FindTopOfStack(info
. THREAD_FLD (r1
));
233 GC_push_one(info
. THREAD_FLD (r0
));
234 GC_push_one(info
. THREAD_FLD (r2
));
235 GC_push_one(info
. THREAD_FLD (r3
));
236 GC_push_one(info
. THREAD_FLD (r4
));
237 GC_push_one(info
. THREAD_FLD (r5
));
238 GC_push_one(info
. THREAD_FLD (r6
));
239 GC_push_one(info
. THREAD_FLD (r7
));
240 GC_push_one(info
. THREAD_FLD (r8
));
241 GC_push_one(info
. THREAD_FLD (r9
));
242 GC_push_one(info
. THREAD_FLD (r10
));
243 GC_push_one(info
. THREAD_FLD (r11
));
244 GC_push_one(info
. THREAD_FLD (r12
));
245 GC_push_one(info
. THREAD_FLD (r13
));
246 GC_push_one(info
. THREAD_FLD (r14
));
247 GC_push_one(info
. THREAD_FLD (r15
));
248 GC_push_one(info
. THREAD_FLD (r16
));
249 GC_push_one(info
. THREAD_FLD (r17
));
250 GC_push_one(info
. THREAD_FLD (r18
));
251 GC_push_one(info
. THREAD_FLD (r19
));
252 GC_push_one(info
. THREAD_FLD (r20
));
253 GC_push_one(info
. THREAD_FLD (r21
));
254 GC_push_one(info
. THREAD_FLD (r22
));
255 GC_push_one(info
. THREAD_FLD (r23
));
256 GC_push_one(info
. THREAD_FLD (r24
));
257 GC_push_one(info
. THREAD_FLD (r25
));
258 GC_push_one(info
. THREAD_FLD (r26
));
259 GC_push_one(info
. THREAD_FLD (r27
));
260 GC_push_one(info
. THREAD_FLD (r28
));
261 GC_push_one(info
. THREAD_FLD (r29
));
262 GC_push_one(info
. THREAD_FLD (r30
));
263 GC_push_one(info
. THREAD_FLD (r31
));
265 /* FIXME: Remove after testing: */
266 WARN("This is completely untested and likely will not work\n", 0);
268 mach_msg_type_number_t outCount
= THREAD_STATE_MAX
;
269 r
= thread_get_state(thread
, MACHINE_THREAD_STATE
,
270 (natural_t
*)&info
, &outCount
);
271 if(r
!= KERN_SUCCESS
) ABORT("task_get_state failed");
273 lo
= (void*)info
. THREAD_FLD (esp
);
274 hi
= (ptr_t
)FindTopOfStack(info
. THREAD_FLD (esp
));
276 GC_push_one(info
. THREAD_FLD (eax
));
277 GC_push_one(info
. THREAD_FLD (ebx
));
278 GC_push_one(info
. THREAD_FLD (ecx
));
279 GC_push_one(info
. THREAD_FLD (edx
));
280 GC_push_one(info
. THREAD_FLD (edi
));
281 GC_push_one(info
. THREAD_FLD (esi
));
282 /* GC_push_one(info . THREAD_FLD (ebp)); */
283 /* GC_push_one(info . THREAD_FLD (esp)); */
284 GC_push_one(info
. THREAD_FLD (ss
));
285 GC_push_one(info
. THREAD_FLD (eip
));
286 GC_push_one(info
. THREAD_FLD (cs
));
287 GC_push_one(info
. THREAD_FLD (ds
));
288 GC_push_one(info
. THREAD_FLD (es
));
289 GC_push_one(info
. THREAD_FLD (fs
));
290 GC_push_one(info
. THREAD_FLD (gs
));
291 # endif /* !POWERPC */
294 GC_printf3("Darwin: Stack for thread 0x%lx = [%lx,%lx)\n",
295 (unsigned long) thread
,
300 GC_push_all_stack(lo
, hi
);
301 } /* for(p=GC_threads[i]...) */
302 vm_deallocate(current_task(), (vm_address_t
)act_list
, sizeof(thread_t
) * listcount
);
304 #endif /* !DARWIN_DONT_PARSE_STACK */
306 static mach_port_t GC_mach_handler_thread
;
307 static int GC_use_mach_handler_thread
= 0;
309 static struct GC_mach_thread GC_mach_threads
[THREAD_TABLE_SZ
];
310 static int GC_mach_threads_count
;
312 void GC_stop_init() {
315 for (i
= 0; i
< THREAD_TABLE_SZ
; i
++) {
316 GC_mach_threads
[i
].thread
= 0;
317 GC_mach_threads
[i
].already_suspended
= 0;
319 GC_mach_threads_count
= 0;
322 /* returns true if there's a thread in act_list that wasn't in old_list */
323 int GC_suspend_thread_list(thread_act_array_t act_list
, int count
,
324 thread_act_array_t old_list
, int old_count
) {
325 mach_port_t my_thread
= mach_thread_self();
330 for(i
= 0; i
< count
; i
++) {
331 thread_act_t thread
= act_list
[i
];
333 GC_printf1("Attempting to suspend thread %p\n", thread
);
335 /* find the current thread in the old list */
337 for(j
= 0; j
< old_count
; j
++) {
338 thread_act_t old_thread
= old_list
[j
];
339 if (old_thread
== thread
) {
345 /* add it to the GC_mach_threads list */
346 GC_mach_threads
[GC_mach_threads_count
].thread
= thread
;
347 /* default is not suspended */
348 GC_mach_threads
[GC_mach_threads_count
].already_suspended
= 0;
352 if (thread
!= my_thread
&&
353 (!GC_use_mach_handler_thread
354 || (GC_use_mach_handler_thread
355 && GC_mach_handler_thread
!= thread
))) {
356 struct thread_basic_info info
;
357 mach_msg_type_number_t outCount
= THREAD_INFO_MAX
;
358 kern_return_t kern_result
= thread_info(thread
, THREAD_BASIC_INFO
,
359 (thread_info_t
)&info
, &outCount
);
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 GC_printf2("Thread state for 0x%lx = %d\n", thread
, info
.run_state
);
374 GC_mach_threads
[GC_mach_threads_count
].already_suspended
= info
.suspend_count
;
376 if (info
.suspend_count
) continue;
379 GC_printf1("Suspending 0x%lx\n", thread
);
381 /* Suspend the thread */
382 kern_result
= thread_suspend(thread
);
383 if(kern_result
!= KERN_SUCCESS
) {
384 /* the thread may have quit since the thread_threads () call
385 * we mark already_suspended so it's not dealt with anymore later
388 GC_mach_threads
[GC_mach_threads_count
].already_suspended
= TRUE
;
389 GC_mach_threads_count
++;
394 if (!found
) GC_mach_threads_count
++;
400 /* Caller holds allocation lock. */
405 mach_port_t my_thread
= mach_thread_self();
406 kern_return_t kern_result
;
407 thread_act_array_t act_list
, prev_list
;
408 mach_msg_type_number_t listcount
, prevcount
;
411 GC_printf1("Stopping the world from 0x%lx\n", mach_thread_self());
414 /* clear out the mach threads list table */
417 /* Make sure all free list construction has stopped before we start. */
418 /* No new construction can start, since free list construction is */
419 /* required to acquire and release the GC lock before it starts, */
420 /* and we have the lock. */
421 # ifdef PARALLEL_MARK
422 GC_acquire_mark_lock();
423 GC_ASSERT(GC_fl_builder_count
== 0);
424 /* We should have previously waited for it to become zero. */
425 # endif /* PARALLEL_MARK */
427 /* Loop stopping threads until you have gone over the whole list
428 twice without a new one appearing. thread_create() won't
429 return (and thus the thread stop) until the new thread
430 exists, so there is no window whereby you could stop a
431 thread, recognise it is stopped, but then have a new thread
432 it created before stopping show up later.
440 kern_result
= task_threads(current_task(), &act_list
, &listcount
);
441 result
= GC_suspend_thread_list(act_list
, listcount
,
442 prev_list
, prevcount
);
444 prev_list
= act_list
;
445 prevcount
= listcount
;
446 vm_deallocate(current_task(), (vm_address_t
)act_list
, sizeof(thread_t
) * listcount
);
452 extern void GC_mprotect_stop();
457 # ifdef PARALLEL_MARK
458 GC_release_mark_lock();
461 GC_printf1("World stopped from 0x%lx\n", my_thread
);
465 /* Caller holds allocation lock, and has held it continuously since */
466 /* the world stopped. */
467 void GC_start_world()
469 mach_port_t my_thread
= mach_thread_self();
472 kern_return_t kern_result
;
473 thread_act_array_t act_list
;
474 mach_msg_type_number_t listcount
;
475 struct thread_basic_info info
;
476 mach_msg_type_number_t outCount
= THREAD_INFO_MAX
;
479 GC_printf0("World starting\n");
484 extern void GC_mprotect_resume();
485 GC_mprotect_resume();
489 kern_result
= task_threads(current_task(), &act_list
, &listcount
);
490 for(i
= 0; i
< listcount
; i
++) {
491 thread_act_t thread
= act_list
[i
];
492 if (thread
!= my_thread
&&
493 (!GC_use_mach_handler_thread
||
494 (GC_use_mach_handler_thread
&& GC_mach_handler_thread
!= thread
))) {
495 for(j
= 0; j
< GC_mach_threads_count
; j
++) {
496 if (thread
== GC_mach_threads
[j
].thread
) {
497 if (GC_mach_threads
[j
].already_suspended
) {
499 GC_printf1("Not resuming already suspended thread %p\n", thread
);
503 kern_result
= thread_info(thread
, THREAD_BASIC_INFO
,
504 (thread_info_t
)&info
, &outCount
);
505 if(kern_result
!= KERN_SUCCESS
) ABORT("thread_info failed");
507 GC_printf2("Thread state for 0x%lx = %d\n", thread
,
509 GC_printf1("Resuming 0x%lx\n", thread
);
511 /* Resume the thread */
512 kern_result
= thread_resume(thread
);
513 if(kern_result
!= KERN_SUCCESS
) ABORT("thread_resume failed");
518 vm_deallocate(current_task(), (vm_address_t
)act_list
, sizeof(thread_t
) * listcount
);
520 GC_printf0("World started\n");
524 void GC_darwin_register_mach_handler_thread(mach_port_t thread
) {
525 GC_mach_handler_thread
= thread
;
526 GC_use_mach_handler_thread
= 1;