2 * Copyright (c) 1994 by Xerox Corporation. All rights reserved.
3 * Copyright (c) 1996 by Silicon Graphics. All rights reserved.
4 * Copyright (c) 1998 by Fergus Henderson. All rights reserved.
5 * Copyright (c) 2000-2010 by Hewlett-Packard Development Company.
8 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
9 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
11 * Permission is hereby granted to use or copy this program
12 * for any purpose, provided the above notices are retained on all copies.
13 * Permission to modify the code and to distribute modified code is granted,
14 * provided the above notices are retained, and a notice that the code was
15 * modified is included with the above copyright notice.
18 #include "private/pthread_support.h"
20 /* This probably needs more porting work to ppc64. */
22 #if defined(GC_DARWIN_THREADS)
24 /* From "Inside Mac OS X - Mach-O Runtime Architecture" published by Apple
26 "The space beneath the stack pointer, where a new stack frame would normally
27 be allocated, is called the red zone. This area as shown in Figure 3-2 may
28 be used for any purpose as long as a new stack frame does not need to be
31 Page 50: "If a leaf procedure's red zone usage would exceed 224 bytes, then
32 it must set up a stack frame just like routines that call other routines."
36 # define PPC_RED_ZONE_SIZE 224
37 # elif CPP_WORDSZ == 64
38 # define PPC_RED_ZONE_SIZE 320
42 #ifndef DARWIN_DONT_PARSE_STACK
44 typedef struct StackFrame
{
45 unsigned long savedSP
;
46 unsigned long savedCR
;
47 unsigned long savedLR
;
48 unsigned long reserved
[2];
49 unsigned long savedRTOC
;
52 GC_INNER ptr_t
GC_FindTopOfStack(unsigned long stack_start
)
57 if (stack_start
== 0) {
59 __asm__
__volatile__ ("lwz %0,0(r1)" : "=r" (frame
));
61 __asm__
__volatile__ ("ld %0,0(r1)" : "=r" (frame
));
65 GC_ASSERT(stack_start
!= 0); /* not implemented */
66 # endif /* !POWERPC */
68 frame
= (StackFrame
*)stack_start
;
72 /* GC_log_printf("FindTopOfStack start at sp = %p\n", frame); */
74 while (frame
->savedSP
!= 0) {
75 /* if there are no more stack frames, stop */
77 frame
= (StackFrame
*)frame
->savedSP
;
79 /* we do these next two checks after going to the next frame
80 because the LR for the first stack frame in the loop
81 is not set up on purpose, so we shouldn't check it. */
82 if ((frame
->savedLR
& ~0x3) == 0 || (frame
->savedLR
& ~0x3) == ~0x3U
)
83 break; /* if the next LR is bogus, stop */
86 /* GC_log_printf("FindTopOfStack finish at sp = %p\n", frame); */
91 #endif /* !DARWIN_DONT_PARSE_STACK */
93 /* GC_query_task_threads controls whether to obtain the list of */
94 /* the threads from the kernel or to use GC_threads table. */
95 #ifdef GC_NO_THREADS_DISCOVERY
96 # define GC_query_task_threads FALSE
97 #elif defined(GC_DISCOVER_TASK_THREADS)
98 # define GC_query_task_threads TRUE
100 STATIC GC_bool GC_query_task_threads
= FALSE
;
101 #endif /* !GC_NO_THREADS_DISCOVERY */
103 /* Use implicit threads registration (all task threads excluding the GC */
104 /* special ones are stoped and scanned). Should be called before */
105 /* GC_INIT() (or, at least, before going multi-threaded). Deprecated. */
106 GC_API
void GC_CALL
GC_use_threads_discovery(void)
108 # if defined(GC_NO_THREADS_DISCOVERY) || defined(DARWIN_DONT_PARSE_STACK)
109 ABORT("Darwin task-threads-based stop and push unsupported");
111 GC_ASSERT(!GC_need_to_lock
);
112 # ifndef GC_DISCOVER_TASK_THREADS
113 GC_query_task_threads
= TRUE
;
115 GC_init_parallel(); /* just to be consistent with Win32 one */
119 /* Evaluates the stack range for a given thread. Returns the lower */
120 /* bound and sets *phi to the upper one. */
121 STATIC ptr_t
GC_stack_range_for(ptr_t
*phi
, thread_act_t thread
, GC_thread p
,
122 GC_bool thread_blocked
, mach_port_t my_thread
)
125 if (thread
== my_thread
) {
126 GC_ASSERT(!thread_blocked
);
128 # ifndef DARWIN_DONT_PARSE_STACK
129 *phi
= GC_FindTopOfStack(0);
132 } else if (thread_blocked
) {
133 lo
= p
->stop_info
.stack_ptr
;
134 # ifndef DARWIN_DONT_PARSE_STACK
135 *phi
= p
->topOfStack
;
139 /* MACHINE_THREAD_STATE_COUNT does not seem to be defined */
140 /* everywhere. Hence we use our own version. Alternatively, */
141 /* we could use THREAD_STATE_MAX (but seems to be not optimal). */
142 kern_return_t kern_result
;
143 mach_msg_type_number_t thread_state_count
= GC_MACH_THREAD_STATE_COUNT
;
144 GC_THREAD_STATE_T state
;
146 /* Get the thread state (registers, etc) */
147 kern_result
= thread_get_state(thread
, GC_MACH_THREAD_STATE
,
149 &thread_state_count
);
150 # ifdef DEBUG_THREADS
151 GC_log_printf("thread_get_state returns value = %d\n", kern_result
);
153 if (kern_result
!= KERN_SUCCESS
)
154 ABORT("thread_get_state failed");
157 lo
= (void *)state
.THREAD_FLD(esp
);
158 # ifndef DARWIN_DONT_PARSE_STACK
159 *phi
= GC_FindTopOfStack(state
.THREAD_FLD(esp
));
161 GC_push_one(state
.THREAD_FLD(eax
));
162 GC_push_one(state
.THREAD_FLD(ebx
));
163 GC_push_one(state
.THREAD_FLD(ecx
));
164 GC_push_one(state
.THREAD_FLD(edx
));
165 GC_push_one(state
.THREAD_FLD(edi
));
166 GC_push_one(state
.THREAD_FLD(esi
));
167 GC_push_one(state
.THREAD_FLD(ebp
));
169 # elif defined(X86_64)
170 lo
= (void *)state
.THREAD_FLD(rsp
);
171 # ifndef DARWIN_DONT_PARSE_STACK
172 *phi
= GC_FindTopOfStack(state
.THREAD_FLD(rsp
));
174 GC_push_one(state
.THREAD_FLD(rax
));
175 GC_push_one(state
.THREAD_FLD(rbx
));
176 GC_push_one(state
.THREAD_FLD(rcx
));
177 GC_push_one(state
.THREAD_FLD(rdx
));
178 GC_push_one(state
.THREAD_FLD(rdi
));
179 GC_push_one(state
.THREAD_FLD(rsi
));
180 GC_push_one(state
.THREAD_FLD(rbp
));
181 /* GC_push_one(state.THREAD_FLD(rsp)); */
182 GC_push_one(state
.THREAD_FLD(r8
));
183 GC_push_one(state
.THREAD_FLD(r9
));
184 GC_push_one(state
.THREAD_FLD(r10
));
185 GC_push_one(state
.THREAD_FLD(r11
));
186 GC_push_one(state
.THREAD_FLD(r12
));
187 GC_push_one(state
.THREAD_FLD(r13
));
188 GC_push_one(state
.THREAD_FLD(r14
));
189 GC_push_one(state
.THREAD_FLD(r15
));
191 # elif defined(POWERPC)
192 lo
= (void *)(state
.THREAD_FLD(r1
) - PPC_RED_ZONE_SIZE
);
193 # ifndef DARWIN_DONT_PARSE_STACK
194 *phi
= GC_FindTopOfStack(state
.THREAD_FLD(r1
));
196 GC_push_one(state
.THREAD_FLD(r0
));
197 GC_push_one(state
.THREAD_FLD(r2
));
198 GC_push_one(state
.THREAD_FLD(r3
));
199 GC_push_one(state
.THREAD_FLD(r4
));
200 GC_push_one(state
.THREAD_FLD(r5
));
201 GC_push_one(state
.THREAD_FLD(r6
));
202 GC_push_one(state
.THREAD_FLD(r7
));
203 GC_push_one(state
.THREAD_FLD(r8
));
204 GC_push_one(state
.THREAD_FLD(r9
));
205 GC_push_one(state
.THREAD_FLD(r10
));
206 GC_push_one(state
.THREAD_FLD(r11
));
207 GC_push_one(state
.THREAD_FLD(r12
));
208 GC_push_one(state
.THREAD_FLD(r13
));
209 GC_push_one(state
.THREAD_FLD(r14
));
210 GC_push_one(state
.THREAD_FLD(r15
));
211 GC_push_one(state
.THREAD_FLD(r16
));
212 GC_push_one(state
.THREAD_FLD(r17
));
213 GC_push_one(state
.THREAD_FLD(r18
));
214 GC_push_one(state
.THREAD_FLD(r19
));
215 GC_push_one(state
.THREAD_FLD(r20
));
216 GC_push_one(state
.THREAD_FLD(r21
));
217 GC_push_one(state
.THREAD_FLD(r22
));
218 GC_push_one(state
.THREAD_FLD(r23
));
219 GC_push_one(state
.THREAD_FLD(r24
));
220 GC_push_one(state
.THREAD_FLD(r25
));
221 GC_push_one(state
.THREAD_FLD(r26
));
222 GC_push_one(state
.THREAD_FLD(r27
));
223 GC_push_one(state
.THREAD_FLD(r28
));
224 GC_push_one(state
.THREAD_FLD(r29
));
225 GC_push_one(state
.THREAD_FLD(r30
));
226 GC_push_one(state
.THREAD_FLD(r31
));
228 # elif defined(ARM32)
229 lo
= (void *)state
.__sp
;
230 # ifndef DARWIN_DONT_PARSE_STACK
231 *phi
= GC_FindTopOfStack(state
.__sp
);
233 GC_push_one(state
.__r
[0]);
234 GC_push_one(state
.__r
[1]);
235 GC_push_one(state
.__r
[2]);
236 GC_push_one(state
.__r
[3]);
237 GC_push_one(state
.__r
[4]);
238 GC_push_one(state
.__r
[5]);
239 GC_push_one(state
.__r
[6]);
240 GC_push_one(state
.__r
[7]);
241 GC_push_one(state
.__r
[8]);
242 GC_push_one(state
.__r
[9]);
243 GC_push_one(state
.__r
[10]);
244 GC_push_one(state
.__r
[11]);
245 GC_push_one(state
.__r
[12]);
246 /* GC_push_one(state.__sp); */
247 GC_push_one(state
.__lr
);
248 /* GC_push_one(state.__pc); */
249 GC_push_one(state
.__cpsr
);
252 # error FIXME for non-x86 || ppc || arm architectures
254 } /* thread != my_thread */
256 # ifdef DARWIN_DONT_PARSE_STACK
257 /* p is guaranteed to be non-NULL regardless of GC_query_task_threads. */
258 *phi
= (p
->flags
& MAIN_THREAD
) != 0 ? GC_stackbottom
: p
->stack_end
;
260 # ifdef DEBUG_THREADS
261 GC_log_printf("Darwin: Stack for thread %p = [%p,%p)\n",
262 (void *)thread
, lo
, *phi
);
267 GC_INNER
void GC_push_all_stacks(void)
271 task_t my_task
= current_task();
272 mach_port_t my_thread
= mach_thread_self();
273 GC_bool found_me
= FALSE
;
276 mach_msg_type_number_t listcount
= (mach_msg_type_number_t
)THREAD_TABLE_SZ
;
277 if (!EXPECT(GC_thr_initialized
, TRUE
))
280 # ifndef DARWIN_DONT_PARSE_STACK
281 if (GC_query_task_threads
) {
282 kern_return_t kern_result
;
283 thread_act_array_t act_list
= 0;
285 /* Obtain the list of the threads from the kernel. */
286 kern_result
= task_threads(my_task
, &act_list
, &listcount
);
287 if (kern_result
!= KERN_SUCCESS
)
288 ABORT("task_threads failed");
290 for (i
= 0; i
< (int)listcount
; i
++) {
291 thread_act_t thread
= act_list
[i
];
292 lo
= GC_stack_range_for(&hi
, thread
, NULL
, FALSE
, my_thread
);
293 GC_ASSERT((word
)lo
<= (word
)hi
);
294 total_size
+= hi
- lo
;
295 GC_push_all_stack(lo
, hi
);
297 if (thread
== my_thread
)
299 mach_port_deallocate(my_task
, thread
);
300 } /* for (i=0; ...) */
302 vm_deallocate(my_task
, (vm_address_t
)act_list
,
303 sizeof(thread_t
) * listcount
);
305 # endif /* !DARWIN_DONT_PARSE_STACK */
307 for (i
= 0; i
< (int)listcount
; i
++) {
309 for (p
= GC_threads
[i
]; p
!= NULL
; p
= p
->next
)
310 if ((p
->flags
& FINISHED
) == 0) {
311 thread_act_t thread
= (thread_act_t
)p
->stop_info
.mach_thread
;
312 lo
= GC_stack_range_for(&hi
, thread
, p
, (GC_bool
)p
->thread_blocked
,
314 GC_ASSERT((word
)lo
<= (word
)hi
);
315 total_size
+= hi
- lo
;
316 GC_push_all_stack_sections(lo
, hi
, p
->traced_stack_sect
);
318 if (thread
== my_thread
)
321 } /* for (i=0; ...) */
324 mach_port_deallocate(my_task
, my_thread
);
325 GC_VERBOSE_LOG_PRINTF("Pushed %d thread stacks\n", nthreads
);
326 if (!found_me
&& !GC_in_thread_creation
)
327 ABORT("Collecting from unknown thread");
328 GC_total_stacksize
= total_size
;
331 #ifndef GC_NO_THREADS_DISCOVERY
334 STATIC mach_port_t GC_mach_handler_thread
= 0;
335 STATIC GC_bool GC_use_mach_handler_thread
= FALSE
;
337 GC_INNER
void GC_darwin_register_mach_handler_thread(mach_port_t thread
)
339 GC_mach_handler_thread
= thread
;
340 GC_use_mach_handler_thread
= TRUE
;
342 # endif /* MPROTECT_VDB */
344 # ifndef GC_MAX_MACH_THREADS
345 # define GC_MAX_MACH_THREADS THREAD_TABLE_SZ
348 struct GC_mach_thread
{
350 GC_bool already_suspended
;
353 struct GC_mach_thread GC_mach_threads
[GC_MAX_MACH_THREADS
];
354 STATIC
int GC_mach_threads_count
= 0;
355 /* FIXME: it is better to implement GC_mach_threads as a hash set. */
357 /* returns true if there's a thread in act_list that wasn't in old_list */
358 STATIC GC_bool
GC_suspend_thread_list(thread_act_array_t act_list
, int count
,
359 thread_act_array_t old_list
,
360 int old_count
, mach_port_t my_thread
)
364 GC_bool changed
= FALSE
;
366 for (i
= 0; i
< count
; i
++) {
367 thread_act_t thread
= act_list
[i
];
369 struct thread_basic_info info
;
370 mach_msg_type_number_t outCount
;
371 kern_return_t kern_result
;
373 if (thread
== my_thread
375 || (GC_mach_handler_thread
== thread
&& GC_use_mach_handler_thread
)
378 /* Don't add our and the handler threads. */
381 # ifdef PARALLEL_MARK
382 if (GC_is_mach_marker(thread
))
383 continue; /* ignore the parallel marker threads */
386 # ifdef DEBUG_THREADS
387 GC_log_printf("Attempting to suspend thread %p\n", (void *)thread
);
389 /* find the current thread in the old list */
392 int last_found
= j
; /* remember the previous found thread index */
394 /* Search for the thread starting from the last found one first. */
395 while (++j
< old_count
)
396 if (old_list
[j
] == thread
) {
401 /* If not found, search in the rest (beginning) of the list. */
402 for (j
= 0; j
< last_found
; j
++)
403 if (old_list
[j
] == thread
) {
409 /* add it to the GC_mach_threads list */
410 if (GC_mach_threads_count
== GC_MAX_MACH_THREADS
)
411 ABORT("Too many threads");
412 GC_mach_threads
[GC_mach_threads_count
].thread
= thread
;
413 /* default is not suspended */
414 GC_mach_threads
[GC_mach_threads_count
].already_suspended
= FALSE
;
420 outCount
= THREAD_INFO_MAX
;
421 kern_result
= thread_info(thread
, THREAD_BASIC_INFO
,
422 (thread_info_t
)&info
, &outCount
);
423 if (kern_result
!= KERN_SUCCESS
) {
424 /* The thread may have quit since the thread_threads() call we */
425 /* mark already suspended so it's not dealt with anymore later. */
427 GC_mach_threads
[GC_mach_threads_count
++].already_suspended
= TRUE
;
430 # ifdef DEBUG_THREADS
431 GC_log_printf("Thread state for %p = %d\n", (void *)thread
, info
.run_state
);
433 if (info
.suspend_count
!= 0) {
434 /* thread is already suspended. */
436 GC_mach_threads
[GC_mach_threads_count
++].already_suspended
= TRUE
;
440 # ifdef DEBUG_THREADS
441 GC_log_printf("Suspending %p\n", (void *)thread
);
443 kern_result
= thread_suspend(thread
);
444 if (kern_result
!= KERN_SUCCESS
) {
445 /* The thread may have quit since the thread_threads() call we */
446 /* mark already suspended so it's not dealt with anymore later. */
448 GC_mach_threads
[GC_mach_threads_count
++].already_suspended
= TRUE
;
452 GC_mach_threads_count
++;
457 #endif /* !GC_NO_THREADS_DISCOVERY */
459 /* Caller holds allocation lock. */
460 GC_INNER
void GC_stop_world(void)
463 task_t my_task
= current_task();
464 mach_port_t my_thread
= mach_thread_self();
465 kern_return_t kern_result
;
467 # ifdef DEBUG_THREADS
468 GC_log_printf("Stopping the world from thread %p\n", (void *)my_thread
);
470 # ifdef PARALLEL_MARK
472 /* Make sure all free list construction has stopped before we */
473 /* start. No new construction can start, since free list */
474 /* construction is required to acquire and release the GC lock */
475 /* before it starts, and we have the lock. */
476 GC_acquire_mark_lock();
477 GC_ASSERT(GC_fl_builder_count
== 0);
478 /* We should have previously waited for it to become zero. */
480 # endif /* PARALLEL_MARK */
482 if (GC_query_task_threads
) {
483 # ifndef GC_NO_THREADS_DISCOVERY
485 thread_act_array_t act_list
, prev_list
;
486 mach_msg_type_number_t listcount
, prevcount
;
488 /* Clear out the mach threads list table. We do not need to */
489 /* really clear GC_mach_threads[] as it is used only in the range */
490 /* from 0 to GC_mach_threads_count-1, inclusive. */
491 GC_mach_threads_count
= 0;
493 /* Loop stopping threads until you have gone over the whole list */
494 /* twice without a new one appearing. thread_create() won't */
495 /* return (and thus the thread stop) until the new thread exists, */
496 /* so there is no window whereby you could stop a thread, */
497 /* recognize it is stopped, but then have a new thread it created */
498 /* before stopping show up later. */
503 kern_result
= task_threads(my_task
, &act_list
, &listcount
);
505 if (kern_result
== KERN_SUCCESS
) {
506 changed
= GC_suspend_thread_list(act_list
, listcount
, prev_list
,
507 prevcount
, my_thread
);
509 if (prev_list
!= NULL
) {
510 for (i
= 0; i
< prevcount
; i
++)
511 mach_port_deallocate(my_task
, prev_list
[i
]);
513 vm_deallocate(my_task
, (vm_address_t
)prev_list
,
514 sizeof(thread_t
) * prevcount
);
517 /* Repeat while having changes. */
518 prev_list
= act_list
;
519 prevcount
= listcount
;
523 GC_ASSERT(prev_list
!= 0);
524 for (i
= 0; i
< prevcount
; i
++)
525 mach_port_deallocate(my_task
, prev_list
[i
]);
526 vm_deallocate(my_task
, (vm_address_t
)act_list
,
527 sizeof(thread_t
) * listcount
);
528 # endif /* !GC_NO_THREADS_DISCOVERY */
531 for (i
= 0; i
< THREAD_TABLE_SZ
; i
++) {
534 for (p
= GC_threads
[i
]; p
!= NULL
; p
= p
->next
) {
535 if ((p
->flags
& FINISHED
) == 0 && !p
->thread_blocked
&&
536 p
->stop_info
.mach_thread
!= my_thread
) {
538 kern_result
= thread_suspend(p
->stop_info
.mach_thread
);
539 if (kern_result
!= KERN_SUCCESS
)
540 ABORT("thread_suspend failed");
551 # ifdef PARALLEL_MARK
553 GC_release_mark_lock();
556 # ifdef DEBUG_THREADS
557 GC_log_printf("World stopped from %p\n", (void *)my_thread
);
559 mach_port_deallocate(my_task
, my_thread
);
562 GC_INLINE
void GC_thread_resume(thread_act_t thread
)
564 kern_return_t kern_result
;
565 # if defined(DEBUG_THREADS) || defined(GC_ASSERTIONS)
566 struct thread_basic_info info
;
567 mach_msg_type_number_t outCount
= THREAD_INFO_MAX
;
568 kern_result
= thread_info(thread
, THREAD_BASIC_INFO
,
569 (thread_info_t
)&info
, &outCount
);
570 if (kern_result
!= KERN_SUCCESS
)
571 ABORT("thread_info failed");
573 # ifdef DEBUG_THREADS
574 GC_log_printf("Resuming thread %p with state %d\n", (void *)thread
,
577 /* Resume the thread */
578 kern_result
= thread_resume(thread
);
579 if (kern_result
!= KERN_SUCCESS
)
580 ABORT("thread_resume failed");
583 /* Caller holds allocation lock, and has held it continuously since */
584 /* the world stopped. */
585 GC_INNER
void GC_start_world(void)
587 task_t my_task
= current_task();
589 # ifdef DEBUG_THREADS
590 GC_log_printf("World starting\n");
594 GC_mprotect_resume();
598 if (GC_query_task_threads
) {
599 # ifndef GC_NO_THREADS_DISCOVERY
600 int j
= GC_mach_threads_count
;
601 kern_return_t kern_result
;
602 thread_act_array_t act_list
;
603 mach_msg_type_number_t listcount
;
605 kern_result
= task_threads(my_task
, &act_list
, &listcount
);
606 if (kern_result
!= KERN_SUCCESS
)
607 ABORT("task_threads failed");
609 for (i
= 0; i
< (int)listcount
; i
++) {
610 thread_act_t thread
= act_list
[i
];
611 int last_found
= j
; /* The thread index found during the */
612 /* previous iteration (count value */
613 /* means no thread found yet). */
615 /* Search for the thread starting from the last found one first. */
616 while (++j
< GC_mach_threads_count
) {
617 if (GC_mach_threads
[j
].thread
== thread
)
620 if (j
>= GC_mach_threads_count
) {
621 /* If not found, search in the rest (beginning) of the list. */
622 for (j
= 0; j
< last_found
; j
++) {
623 if (GC_mach_threads
[j
].thread
== thread
)
628 if (j
!= last_found
) {
629 /* The thread is found in GC_mach_threads. */
630 if (GC_mach_threads
[j
].already_suspended
) {
631 # ifdef DEBUG_THREADS
632 GC_log_printf("Not resuming already suspended thread %p\n",
636 GC_thread_resume(thread
);
640 mach_port_deallocate(my_task
, thread
);
642 vm_deallocate(my_task
, (vm_address_t
)act_list
,
643 sizeof(thread_t
) * listcount
);
644 # endif /* !GC_NO_THREADS_DISCOVERY */
647 mach_port_t my_thread
= mach_thread_self();
649 for (i
= 0; i
< THREAD_TABLE_SZ
; i
++) {
651 for (p
= GC_threads
[i
]; p
!= NULL
; p
= p
->next
) {
652 if ((p
->flags
& FINISHED
) == 0 && !p
->thread_blocked
&&
653 p
->stop_info
.mach_thread
!= my_thread
)
654 GC_thread_resume(p
->stop_info
.mach_thread
);
658 mach_port_deallocate(my_task
, my_thread
);
661 # ifdef DEBUG_THREADS
662 GC_log_printf("World started\n");
666 #endif /* GC_DARWIN_THREADS */