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)
56 if (stack_start == 0) {
59 __asm__ __volatile__ ("lwz %0,0(r1)" : "=r" (frame));
61 __asm__ __volatile__ ("ld %0,0(r1)" : "=r" (frame));
65 frame = (StackFrame *)stack_start;
69 /* GC_printf("FindTopOfStack start at sp = %p\n", frame); */
71 while (frame->savedSP != 0) {
72 /* if there are no more stack frames, stop */
74 frame = (StackFrame*)frame->savedSP;
76 /* we do these next two checks after going to the next frame
77 because the LR for the first stack frame in the loop
78 is not set up on purpose, so we shouldn't check it. */
79 if ((frame->savedLR & ~0x3) == 0 || (frame->savedLR & ~0x3) == ~0x3)
80 break; /* if the next LR is bogus, stop */
83 /* GC_printf("FindTopOfStack finish at sp = %p\n", frame); */
88 #endif /* !DARWIN_DONT_PARSE_STACK */
90 /* GC_query_task_threads controls whether to obtain the list of */
91 /* the threads from the kernel or to use GC_threads table. */
92 #ifdef GC_NO_THREADS_DISCOVERY
93 # define GC_query_task_threads FALSE
94 #elif defined(GC_DISCOVER_TASK_THREADS)
95 # define GC_query_task_threads TRUE
97 STATIC GC_bool GC_query_task_threads = FALSE;
98 #endif /* !GC_NO_THREADS_DISCOVERY */
100 /* Use implicit threads registration (all task threads excluding the GC */
101 /* special ones are stoped and scanned). Should be called before */
102 /* GC_INIT() (or, at least, before going multi-threaded). Deprecated. */
103 GC_API void GC_CALL GC_use_threads_discovery(void)
105 # if defined(GC_NO_THREADS_DISCOVERY) || defined(DARWIN_DONT_PARSE_STACK)
106 ABORT("Darwin task-threads-based stop and push unsupported");
108 GC_ASSERT(!GC_need_to_lock);
109 # ifndef GC_DISCOVER_TASK_THREADS
110 GC_query_task_threads = TRUE;
112 GC_init_parallel(); /* just to be consistent with Win32 one */
116 /* Evaluates the stack range for a given thread. Returns the lower */
117 /* bound and sets *phi to the upper one. */
118 STATIC ptr_t GC_stack_range_for(ptr_t *phi, thread_act_t thread, GC_thread p,
119 GC_bool thread_blocked, mach_port_t my_thread)
122 if (thread == my_thread) {
123 GC_ASSERT(!thread_blocked);
125 # ifndef DARWIN_DONT_PARSE_STACK
126 *phi = GC_FindTopOfStack(0);
129 } else if (thread_blocked) {
130 lo = p->stop_info.stack_ptr;
131 # ifndef DARWIN_DONT_PARSE_STACK
132 *phi = p->topOfStack;
136 /* MACHINE_THREAD_STATE_COUNT does not seem to be defined */
137 /* everywhere. Hence we use our own version. Alternatively, */
138 /* we could use THREAD_STATE_MAX (but seems to be not optimal). */
139 kern_return_t kern_result;
140 mach_msg_type_number_t thread_state_count = GC_MACH_THREAD_STATE_COUNT;
141 GC_THREAD_STATE_T state;
143 /* Get the thread state (registers, etc) */
144 kern_result = thread_get_state(thread, GC_MACH_THREAD_STATE,
146 &thread_state_count);
147 # ifdef DEBUG_THREADS
148 GC_log_printf("thread_get_state returns value = %d\n", kern_result);
150 if (kern_result != KERN_SUCCESS)
151 ABORT("thread_get_state failed");
154 lo = (void *)state.THREAD_FLD(esp);
155 # ifndef DARWIN_DONT_PARSE_STACK
156 *phi = GC_FindTopOfStack(state.THREAD_FLD(esp));
158 GC_push_one(state.THREAD_FLD(eax));
159 GC_push_one(state.THREAD_FLD(ebx));
160 GC_push_one(state.THREAD_FLD(ecx));
161 GC_push_one(state.THREAD_FLD(edx));
162 GC_push_one(state.THREAD_FLD(edi));
163 GC_push_one(state.THREAD_FLD(esi));
164 GC_push_one(state.THREAD_FLD(ebp));
166 # elif defined(X86_64)
167 lo = (void *)state.THREAD_FLD(rsp);
168 # ifndef DARWIN_DONT_PARSE_STACK
169 *phi = GC_FindTopOfStack(state.THREAD_FLD(rsp));
171 GC_push_one(state.THREAD_FLD(rax));
172 GC_push_one(state.THREAD_FLD(rbx));
173 GC_push_one(state.THREAD_FLD(rcx));
174 GC_push_one(state.THREAD_FLD(rdx));
175 GC_push_one(state.THREAD_FLD(rdi));
176 GC_push_one(state.THREAD_FLD(rsi));
177 GC_push_one(state.THREAD_FLD(rbp));
178 /* GC_push_one(state.THREAD_FLD(rsp)); */
179 GC_push_one(state.THREAD_FLD(r8));
180 GC_push_one(state.THREAD_FLD(r9));
181 GC_push_one(state.THREAD_FLD(r10));
182 GC_push_one(state.THREAD_FLD(r11));
183 GC_push_one(state.THREAD_FLD(r12));
184 GC_push_one(state.THREAD_FLD(r13));
185 GC_push_one(state.THREAD_FLD(r14));
186 GC_push_one(state.THREAD_FLD(r15));
188 # elif defined(POWERPC)
189 lo = (void *)(state.THREAD_FLD(r1) - PPC_RED_ZONE_SIZE);
190 # ifndef DARWIN_DONT_PARSE_STACK
191 *phi = GC_FindTopOfStack(state.THREAD_FLD(r1));
193 GC_push_one(state.THREAD_FLD(r0));
194 GC_push_one(state.THREAD_FLD(r2));
195 GC_push_one(state.THREAD_FLD(r3));
196 GC_push_one(state.THREAD_FLD(r4));
197 GC_push_one(state.THREAD_FLD(r5));
198 GC_push_one(state.THREAD_FLD(r6));
199 GC_push_one(state.THREAD_FLD(r7));
200 GC_push_one(state.THREAD_FLD(r8));
201 GC_push_one(state.THREAD_FLD(r9));
202 GC_push_one(state.THREAD_FLD(r10));
203 GC_push_one(state.THREAD_FLD(r11));
204 GC_push_one(state.THREAD_FLD(r12));
205 GC_push_one(state.THREAD_FLD(r13));
206 GC_push_one(state.THREAD_FLD(r14));
207 GC_push_one(state.THREAD_FLD(r15));
208 GC_push_one(state.THREAD_FLD(r16));
209 GC_push_one(state.THREAD_FLD(r17));
210 GC_push_one(state.THREAD_FLD(r18));
211 GC_push_one(state.THREAD_FLD(r19));
212 GC_push_one(state.THREAD_FLD(r20));
213 GC_push_one(state.THREAD_FLD(r21));
214 GC_push_one(state.THREAD_FLD(r22));
215 GC_push_one(state.THREAD_FLD(r23));
216 GC_push_one(state.THREAD_FLD(r24));
217 GC_push_one(state.THREAD_FLD(r25));
218 GC_push_one(state.THREAD_FLD(r26));
219 GC_push_one(state.THREAD_FLD(r27));
220 GC_push_one(state.THREAD_FLD(r28));
221 GC_push_one(state.THREAD_FLD(r29));
222 GC_push_one(state.THREAD_FLD(r30));
223 GC_push_one(state.THREAD_FLD(r31));
225 # elif defined(ARM32)
226 lo = (void *)state.__sp;
227 # ifndef DARWIN_DONT_PARSE_STACK
228 *phi = GC_FindTopOfStack(state.__sp);
230 GC_push_one(state.__r[0]);
231 GC_push_one(state.__r[1]);
232 GC_push_one(state.__r[2]);
233 GC_push_one(state.__r[3]);
234 GC_push_one(state.__r[4]);
235 GC_push_one(state.__r[5]);
236 GC_push_one(state.__r[6]);
237 GC_push_one(state.__r[7]);
238 GC_push_one(state.__r[8]);
239 GC_push_one(state.__r[9]);
240 GC_push_one(state.__r[10]);
241 GC_push_one(state.__r[11]);
242 GC_push_one(state.__r[12]);
243 /* GC_push_one(state.__sp); */
244 GC_push_one(state.__lr);
245 /* GC_push_one(state.__pc); */
246 GC_push_one(state.__cpsr);
249 # error FIXME for non-x86 || ppc || arm architectures
251 } /* thread != my_thread */
253 # ifdef DARWIN_DONT_PARSE_STACK
254 /* p is guaranteed to be non-NULL regardless of GC_query_task_threads. */
255 *phi = (p->flags & MAIN_THREAD) != 0 ? GC_stackbottom : p->stack_end;
257 # ifdef DEBUG_THREADS
258 GC_log_printf("Darwin: Stack for thread 0x%lx = [%p,%p)\n",
259 (unsigned long)thread, lo, *phi);
264 GC_INNER void GC_push_all_stacks(void)
268 task_t my_task = current_task();
269 mach_port_t my_thread = mach_thread_self();
270 GC_bool found_me = FALSE;
273 mach_msg_type_number_t listcount = (mach_msg_type_number_t)THREAD_TABLE_SZ;
274 if (!GC_thr_initialized)
277 # ifndef DARWIN_DONT_PARSE_STACK
278 if (GC_query_task_threads) {
279 kern_return_t kern_result;
280 thread_act_array_t act_list = 0;
282 /* Obtain the list of the threads from the kernel. */
283 kern_result = task_threads(my_task, &act_list, &listcount);
284 if (kern_result != KERN_SUCCESS)
285 ABORT("task_threads failed");
287 for (i = 0; i < (int)listcount; i++) {
288 thread_act_t thread = act_list[i];
289 lo = GC_stack_range_for(&hi, thread, NULL, FALSE, my_thread);
291 total_size += hi - lo;
292 GC_push_all_stack(lo, hi);
294 if (thread == my_thread)
296 mach_port_deallocate(my_task, thread);
297 } /* for (i=0; ...) */
299 vm_deallocate(my_task, (vm_address_t)act_list,
300 sizeof(thread_t) * listcount);
302 # endif /* !DARWIN_DONT_PARSE_STACK */
304 for (i = 0; i < (int)listcount; i++) {
306 for (p = GC_threads[i]; p != NULL; p = p->next)
307 if ((p->flags & FINISHED) == 0) {
308 thread_act_t thread = (thread_act_t)p->stop_info.mach_thread;
309 lo = GC_stack_range_for(&hi, thread, p, (GC_bool)p->thread_blocked,
312 total_size += hi - lo;
313 GC_push_all_stack_sections(lo, hi, p->traced_stack_sect);
315 if (thread == my_thread)
318 } /* for (i=0; ...) */
321 mach_port_deallocate(my_task, my_thread);
322 if (GC_print_stats == VERBOSE)
323 GC_log_printf("Pushed %d thread stacks\n", nthreads);
324 if (!found_me && !GC_in_thread_creation)
325 ABORT("Collecting from unknown thread");
326 GC_total_stacksize = total_size;
329 #ifndef GC_NO_THREADS_DISCOVERY
332 STATIC mach_port_t GC_mach_handler_thread = 0;
333 STATIC GC_bool GC_use_mach_handler_thread = FALSE;
335 GC_INNER void GC_darwin_register_mach_handler_thread(mach_port_t thread)
337 GC_mach_handler_thread = thread;
338 GC_use_mach_handler_thread = TRUE;
340 # endif /* MPROTECT_VDB */
342 # ifndef GC_MAX_MACH_THREADS
343 # define GC_MAX_MACH_THREADS THREAD_TABLE_SZ
346 struct GC_mach_thread {
348 GC_bool already_suspended;
351 struct GC_mach_thread GC_mach_threads[GC_MAX_MACH_THREADS];
352 STATIC int GC_mach_threads_count = 0;
353 /* FIXME: it is better to implement GC_mach_threads as a hash set. */
355 /* returns true if there's a thread in act_list that wasn't in old_list */
356 STATIC GC_bool GC_suspend_thread_list(thread_act_array_t act_list, int count,
357 thread_act_array_t old_list,
358 int old_count, mach_port_t my_thread)
362 GC_bool changed = FALSE;
364 for (i = 0; i < count; i++) {
365 thread_act_t thread = act_list[i];
367 struct thread_basic_info info;
368 mach_msg_type_number_t outCount;
369 kern_return_t kern_result;
371 if (thread == my_thread
373 || (GC_mach_handler_thread == thread && GC_use_mach_handler_thread)
376 /* Don't add our and the handler threads. */
379 # ifdef PARALLEL_MARK
380 if (GC_is_mach_marker(thread))
381 continue; /* ignore the parallel marker threads */
384 # ifdef DEBUG_THREADS
385 GC_log_printf("Attempting to suspend thread 0x%lx\n",
386 (unsigned long)thread);
388 /* find the current thread in the old list */
391 int last_found = j; /* remember the previous found thread index */
393 /* Search for the thread starting from the last found one first. */
394 while (++j < old_count)
395 if (old_list[j] == thread) {
400 /* If not found, search in the rest (beginning) of the list. */
401 for (j = 0; j < last_found; j++)
402 if (old_list[j] == thread) {
408 /* add it to the GC_mach_threads list */
409 if (GC_mach_threads_count == GC_MAX_MACH_THREADS)
410 ABORT("Too many threads");
411 GC_mach_threads[GC_mach_threads_count].thread = thread;
412 /* default is not suspended */
413 GC_mach_threads[GC_mach_threads_count].already_suspended = FALSE;
419 outCount = THREAD_INFO_MAX;
420 kern_result = thread_info(thread, THREAD_BASIC_INFO,
421 (thread_info_t)&info, &outCount);
422 if (kern_result != KERN_SUCCESS) {
423 /* The thread may have quit since the thread_threads() call we */
424 /* mark already suspended so it's not dealt with anymore later. */
426 GC_mach_threads[GC_mach_threads_count++].already_suspended = TRUE;
429 # ifdef DEBUG_THREADS
430 GC_log_printf("Thread state for 0x%lx = %d\n", (unsigned long)thread,
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 0x%lx\n", (unsigned long)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 0x%lx\n",
469 (unsigned long)my_thread);
471 # ifdef PARALLEL_MARK
473 /* Make sure all free list construction has stopped before we */
474 /* start. No new construction can start, since free list */
475 /* construction is required to acquire and release the GC lock */
476 /* before it starts, and we have the lock. */
477 GC_acquire_mark_lock();
478 GC_ASSERT(GC_fl_builder_count == 0);
479 /* We should have previously waited for it to become zero. */
481 # endif /* PARALLEL_MARK */
483 if (GC_query_task_threads) {
484 # ifndef GC_NO_THREADS_DISCOVERY
486 thread_act_array_t act_list, prev_list;
487 mach_msg_type_number_t listcount, prevcount;
489 /* Clear out the mach threads list table. We do not need to */
490 /* really clear GC_mach_threads[] as it is used only in the range */
491 /* from 0 to GC_mach_threads_count-1, inclusive. */
492 GC_mach_threads_count = 0;
494 /* Loop stopping threads until you have gone over the whole list */
495 /* twice without a new one appearing. thread_create() won't */
496 /* return (and thus the thread stop) until the new thread exists, */
497 /* so there is no window whereby you could stop a thread, */
498 /* recognize it is stopped, but then have a new thread it created */
499 /* before stopping show up later. */
504 kern_result = task_threads(my_task, &act_list, &listcount);
506 if (kern_result == KERN_SUCCESS) {
507 changed = GC_suspend_thread_list(act_list, listcount, prev_list,
508 prevcount, my_thread);
510 if (prev_list != NULL) {
511 for (i = 0; i < prevcount; i++)
512 mach_port_deallocate(my_task, prev_list[i]);
514 vm_deallocate(my_task, (vm_address_t)prev_list,
515 sizeof(thread_t) * prevcount);
518 /* Repeat while having changes. */
519 prev_list = act_list;
520 prevcount = listcount;
524 GC_ASSERT(prev_list != 0);
525 for (i = 0; i < prevcount; i++)
526 mach_port_deallocate(my_task, prev_list[i]);
527 vm_deallocate(my_task, (vm_address_t)act_list,
528 sizeof(thread_t) * listcount);
529 # endif /* !GC_NO_THREADS_DISCOVERY */
532 for (i = 0; i < THREAD_TABLE_SZ; i++) {
535 for (p = GC_threads[i]; p != NULL; p = p->next) {
536 if ((p->flags & FINISHED) == 0 && !p->thread_blocked &&
537 p->stop_info.mach_thread != my_thread) {
539 kern_result = thread_suspend(p->stop_info.mach_thread);
540 if (kern_result != KERN_SUCCESS)
541 ABORT("thread_suspend failed");
552 # ifdef PARALLEL_MARK
554 GC_release_mark_lock();
557 # ifdef DEBUG_THREADS
558 GC_log_printf("World stopped from 0x%lx\n", (unsigned long)my_thread);
560 mach_port_deallocate(my_task, my_thread);
563 GC_INLINE void GC_thread_resume(thread_act_t thread)
565 kern_return_t kern_result;
566 # if defined(DEBUG_THREADS) || defined(GC_ASSERTIONS)
567 struct thread_basic_info info;
568 mach_msg_type_number_t outCount = THREAD_INFO_MAX;
569 kern_result = thread_info(thread, THREAD_BASIC_INFO,
570 (thread_info_t)&info, &outCount);
571 if (kern_result != KERN_SUCCESS)
572 ABORT("thread_info failed");
574 # ifdef DEBUG_THREADS
575 GC_log_printf("Resuming thread 0x%lx with state %d\n",
576 (unsigned long)thread, info.run_state);
578 /* Resume the thread */
579 kern_result = thread_resume(thread);
580 if (kern_result != KERN_SUCCESS)
581 ABORT("thread_resume failed");
584 /* Caller holds allocation lock, and has held it continuously since */
585 /* the world stopped. */
586 GC_INNER void GC_start_world(void)
588 task_t my_task = current_task();
590 # ifdef DEBUG_THREADS
591 GC_log_printf("World starting\n");
595 GC_mprotect_resume();
599 if (GC_query_task_threads) {
600 # ifndef GC_NO_THREADS_DISCOVERY
601 int j = GC_mach_threads_count;
602 kern_return_t kern_result;
603 thread_act_array_t act_list;
604 mach_msg_type_number_t listcount;
606 kern_result = task_threads(my_task, &act_list, &listcount);
607 if (kern_result != KERN_SUCCESS)
608 ABORT("task_threads failed");
610 for (i = 0; i < (int)listcount; i++) {
611 thread_act_t thread = act_list[i];
612 int last_found = j; /* The thread index found during the */
613 /* previous iteration (count value */
614 /* means no thread found yet). */
616 /* Search for the thread starting from the last found one first. */
617 while (++j < GC_mach_threads_count) {
618 if (GC_mach_threads[j].thread == thread)
621 if (j >= GC_mach_threads_count) {
622 /* If not found, search in the rest (beginning) of the list. */
623 for (j = 0; j < last_found; j++) {
624 if (GC_mach_threads[j].thread == thread)
629 if (j != last_found) {
630 /* The thread is found in GC_mach_threads. */
631 if (GC_mach_threads[j].already_suspended) {
632 # ifdef DEBUG_THREADS
633 GC_log_printf("Not resuming already suspended thread 0x%lx\n",
634 (unsigned long)thread);
637 GC_thread_resume(thread);
641 mach_port_deallocate(my_task, thread);
643 vm_deallocate(my_task, (vm_address_t)act_list,
644 sizeof(thread_t) * listcount);
645 # endif /* !GC_NO_THREADS_DISCOVERY */
648 mach_port_t my_thread = mach_thread_self();
650 for (i = 0; i < THREAD_TABLE_SZ; i++) {
652 for (p = GC_threads[i]; p != NULL; p = p->next) {
653 if ((p->flags & FINISHED) == 0 && !p->thread_blocked &&
654 p->stop_info.mach_thread != my_thread)
655 GC_thread_resume(p->stop_info.mach_thread);
659 mach_port_deallocate(my_task, my_thread);
662 # ifdef DEBUG_THREADS
663 GC_log_printf("World started\n");
667 #endif /* GC_DARWIN_THREADS */