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-2009 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 #ifdef DARWIN_DONT_PARSE_STACK
44 GC_INNER void GC_push_all_stacks(void)
52 GC_THREAD_STATE_T state;
53 /* MACHINE_THREAD_STATE_COUNT doesn't seem to be defined everywhere. */
54 /* Hence we use our own version. */
55 mach_msg_type_number_t thread_state_count = GC_MACH_THREAD_STATE_COUNT;
58 if (!GC_thr_initialized)
61 for(i = 0; i < THREAD_TABLE_SZ; i++) {
62 for(p = GC_threads[i]; p != 0; p = p->next) {
63 if(p->flags & FINISHED) continue;
64 if(pthread_equal(p->id, me)) {
67 /* Get the thread state (registers, etc) */
68 r = thread_get_state(p->stop_info.mach_thread, GC_MACH_THREAD_STATE,
69 (natural_t*)&state, &thread_state_count);
72 GC_printf("thread_get_state return value = %d\n", r);
76 ABORT("thread_get_state failed");
79 lo = (void*)state . THREAD_FLD (esp);
80 GC_push_one(state . THREAD_FLD (eax));
81 GC_push_one(state . THREAD_FLD (ebx));
82 GC_push_one(state . THREAD_FLD (ecx));
83 GC_push_one(state . THREAD_FLD (edx));
84 GC_push_one(state . THREAD_FLD (edi));
85 GC_push_one(state . THREAD_FLD (esi));
86 GC_push_one(state . THREAD_FLD (ebp));
88 # elif defined(X86_64)
89 lo = (void*)state . THREAD_FLD (rsp);
90 GC_push_one(state . THREAD_FLD (rax));
91 GC_push_one(state . THREAD_FLD (rbx));
92 GC_push_one(state . THREAD_FLD (rcx));
93 GC_push_one(state . THREAD_FLD (rdx));
94 GC_push_one(state . THREAD_FLD (rdi));
95 GC_push_one(state . THREAD_FLD (rsi));
96 GC_push_one(state . THREAD_FLD (rbp));
97 GC_push_one(state . THREAD_FLD (rsp));
98 GC_push_one(state . THREAD_FLD (r8));
99 GC_push_one(state . THREAD_FLD (r9));
100 GC_push_one(state . THREAD_FLD (r10));
101 GC_push_one(state . THREAD_FLD (r11));
102 GC_push_one(state . THREAD_FLD (r12));
103 GC_push_one(state . THREAD_FLD (r13));
104 GC_push_one(state . THREAD_FLD (r14));
105 GC_push_one(state . THREAD_FLD (r15));
106 GC_push_one(state . THREAD_FLD (rip));
107 GC_push_one(state . THREAD_FLD (rflags));
108 GC_push_one(state . THREAD_FLD (cs));
109 GC_push_one(state . THREAD_FLD (fs));
110 GC_push_one(state . THREAD_FLD (gs));
112 # elif defined(POWERPC)
113 lo = (void*)(state . THREAD_FLD (r1) - PPC_RED_ZONE_SIZE);
115 GC_push_one(state . THREAD_FLD (r0));
116 GC_push_one(state . THREAD_FLD (r2));
117 GC_push_one(state . THREAD_FLD (r3));
118 GC_push_one(state . THREAD_FLD (r4));
119 GC_push_one(state . THREAD_FLD (r5));
120 GC_push_one(state . THREAD_FLD (r6));
121 GC_push_one(state . THREAD_FLD (r7));
122 GC_push_one(state . THREAD_FLD (r8));
123 GC_push_one(state . THREAD_FLD (r9));
124 GC_push_one(state . THREAD_FLD (r10));
125 GC_push_one(state . THREAD_FLD (r11));
126 GC_push_one(state . THREAD_FLD (r12));
127 GC_push_one(state . THREAD_FLD (r13));
128 GC_push_one(state . THREAD_FLD (r14));
129 GC_push_one(state . THREAD_FLD (r15));
130 GC_push_one(state . THREAD_FLD (r16));
131 GC_push_one(state . THREAD_FLD (r17));
132 GC_push_one(state . THREAD_FLD (r18));
133 GC_push_one(state . THREAD_FLD (r19));
134 GC_push_one(state . THREAD_FLD (r20));
135 GC_push_one(state . THREAD_FLD (r21));
136 GC_push_one(state . THREAD_FLD (r22));
137 GC_push_one(state . THREAD_FLD (r23));
138 GC_push_one(state . THREAD_FLD (r24));
139 GC_push_one(state . THREAD_FLD (r25));
140 GC_push_one(state . THREAD_FLD (r26));
141 GC_push_one(state . THREAD_FLD (r27));
142 GC_push_one(state . THREAD_FLD (r28));
143 GC_push_one(state . THREAD_FLD (r29));
144 GC_push_one(state . THREAD_FLD (r30));
145 GC_push_one(state . THREAD_FLD (r31));
147 # elif defined(ARM32)
148 lo = (void*)state.__sp;
150 GC_push_one(state.__r[0]);
151 GC_push_one(state.__r[1]);
152 GC_push_one(state.__r[2]);
153 GC_push_one(state.__r[3]);
154 GC_push_one(state.__r[4]);
155 GC_push_one(state.__r[5]);
156 GC_push_one(state.__r[6]);
157 GC_push_one(state.__r[7]);
158 GC_push_one(state.__r[8]);
159 GC_push_one(state.__r[9]);
160 GC_push_one(state.__r[10]);
161 GC_push_one(state.__r[11]);
162 GC_push_one(state.__r[12]);
163 /* GC_push_one(state.__sp); */
164 GC_push_one(state.__lr);
165 GC_push_one(state.__pc);
166 GC_push_one(state.__cpsr);
169 # error FIXME for non-x86 || ppc || arm architectures
172 if(p->flags & MAIN_THREAD)
176 # ifdef DEBUG_THREADS
177 GC_printf("Darwin: Stack for thread 0x%lx = [%p,%p)\n",
178 (unsigned long) p -> id, lo, hi);
180 GC_push_all_stack_frames(lo, hi, p -> activation_frame);
181 total_size += hi - lo; /* lo <= hi */
182 } /* for(p=GC_threads[i]...) */
183 } /* for(i=0;i<THREAD_TABLE_SZ...) */
184 GC_total_stacksize = total_size;
187 #else /* !DARWIN_DONT_PARSE_STACK; Use FindTopOfStack() */
189 typedef struct StackFrame {
190 unsigned long savedSP;
191 unsigned long savedCR;
192 unsigned long savedLR;
193 unsigned long reserved[2];
194 unsigned long savedRTOC;
197 static unsigned long FindTopOfStack(unsigned long stack_start)
201 if (stack_start == 0) {
203 # if CPP_WORDSZ == 32
204 __asm__ volatile("lwz %0,0(r1)" : "=r" (frame));
206 __asm__ volatile("ld %0,0(r1)" : "=r" (frame));
210 frame = (StackFrame *)stack_start;
213 # ifdef DEBUG_THREADS
214 /* GC_printf("FindTopOfStack start at sp = %p\n", frame); */
217 if (frame->savedSP == 0)
219 /* if there are no more stack frames, stop */
221 frame = (StackFrame*)frame->savedSP;
223 /* we do these next two checks after going to the next frame
224 because the LR for the first stack frame in the loop
225 is not set up on purpose, so we shouldn't check it. */
226 if ((frame->savedLR & ~3) == 0)
227 break; /* if the next LR is bogus, stop */
228 if ((~(frame->savedLR) & ~3) == 0)
232 # ifdef DEBUG_THREADS
233 /* GC_printf("FindTopOfStack finish at sp = %p\n", frame); */
235 return (unsigned long)frame;
238 GC_INNER void GC_push_all_stacks(void)
246 thread_act_array_t act_list = 0;
247 mach_msg_type_number_t listcount = 0;
249 me = mach_thread_self();
250 if (!GC_thr_initialized)
253 my_task = current_task();
254 r = task_threads(my_task, &act_list, &listcount);
255 if(r != KERN_SUCCESS)
256 ABORT("task_threads failed");
257 for(i = 0; i < listcount; i++) {
258 thread_act_t thread = act_list[i];
261 hi = (ptr_t)FindTopOfStack(0);
263 # if defined(POWERPC)
264 GC_THREAD_STATE_T info;
265 mach_msg_type_number_t outCount = THREAD_STATE_MAX;
266 r = thread_get_state(thread, GC_MACH_THREAD_STATE, (natural_t *)&info,
268 if(r != KERN_SUCCESS)
269 ABORT("task_get_state failed");
271 lo = (void*)(info . THREAD_FLD (r1) - PPC_RED_ZONE_SIZE);
272 hi = (ptr_t)FindTopOfStack(info . THREAD_FLD (r1));
274 GC_push_one(info . THREAD_FLD (r0));
275 GC_push_one(info . THREAD_FLD (r2));
276 GC_push_one(info . THREAD_FLD (r3));
277 GC_push_one(info . THREAD_FLD (r4));
278 GC_push_one(info . THREAD_FLD (r5));
279 GC_push_one(info . THREAD_FLD (r6));
280 GC_push_one(info . THREAD_FLD (r7));
281 GC_push_one(info . THREAD_FLD (r8));
282 GC_push_one(info . THREAD_FLD (r9));
283 GC_push_one(info . THREAD_FLD (r10));
284 GC_push_one(info . THREAD_FLD (r11));
285 GC_push_one(info . THREAD_FLD (r12));
286 GC_push_one(info . THREAD_FLD (r13));
287 GC_push_one(info . THREAD_FLD (r14));
288 GC_push_one(info . THREAD_FLD (r15));
289 GC_push_one(info . THREAD_FLD (r16));
290 GC_push_one(info . THREAD_FLD (r17));
291 GC_push_one(info . THREAD_FLD (r18));
292 GC_push_one(info . THREAD_FLD (r19));
293 GC_push_one(info . THREAD_FLD (r20));
294 GC_push_one(info . THREAD_FLD (r21));
295 GC_push_one(info . THREAD_FLD (r22));
296 GC_push_one(info . THREAD_FLD (r23));
297 GC_push_one(info . THREAD_FLD (r24));
298 GC_push_one(info . THREAD_FLD (r25));
299 GC_push_one(info . THREAD_FLD (r26));
300 GC_push_one(info . THREAD_FLD (r27));
301 GC_push_one(info . THREAD_FLD (r28));
302 GC_push_one(info . THREAD_FLD (r29));
303 GC_push_one(info . THREAD_FLD (r30));
304 GC_push_one(info . THREAD_FLD (r31));
307 /* FIXME: Remove after testing: */
308 WARN("This is completely untested and likely will not work\n", 0);
309 GC_THREAD_STATE_T info;
310 mach_msg_type_number_t outCount = THREAD_STATE_MAX;
311 r = thread_get_state(thread, GC_MACH_THREAD_STATE, (natural_t *)&info,
313 if(r != KERN_SUCCESS)
314 ABORT("task_get_state failed");
316 lo = (void*)info . THREAD_FLD (esp);
317 hi = (ptr_t)FindTopOfStack(info . THREAD_FLD (esp));
319 GC_push_one(info . THREAD_FLD (eax));
320 GC_push_one(info . THREAD_FLD (ebx));
321 GC_push_one(info . THREAD_FLD (ecx));
322 GC_push_one(info . THREAD_FLD (edx));
323 GC_push_one(info . THREAD_FLD (edi));
324 GC_push_one(info . THREAD_FLD (esi));
325 /* GC_push_one(info . THREAD_FLD (ebp)); */
326 /* GC_push_one(info . THREAD_FLD (esp)); */
327 GC_push_one(info . THREAD_FLD (ss));
328 GC_push_one(info . THREAD_FLD (eip));
329 GC_push_one(info . THREAD_FLD (cs));
330 GC_push_one(info . THREAD_FLD (ds));
331 GC_push_one(info . THREAD_FLD (es));
332 GC_push_one(info . THREAD_FLD (fs));
333 GC_push_one(info . THREAD_FLD (gs));
335 # elif defined(X86_64)
336 GC_THREAD_STATE_T info;
337 mach_msg_type_number_t outCount = THREAD_STATE_MAX;
338 r = thread_get_state(thread, GC_MACH_THREAD_STATE, (natural_t *)&info,
340 if(r != KERN_SUCCESS)
341 ABORT("task_get_state failed");
343 lo = (void*)info . THREAD_FLD (rsp);
344 hi = (ptr_t)FindTopOfStack(info . THREAD_FLD (rsp));
346 GC_push_one(info . THREAD_FLD (rax));
347 GC_push_one(info . THREAD_FLD (rbx));
348 GC_push_one(info . THREAD_FLD (rcx));
349 GC_push_one(info . THREAD_FLD (rdx));
350 GC_push_one(info . THREAD_FLD (rdi));
351 GC_push_one(info . THREAD_FLD (rsi));
352 GC_push_one(info . THREAD_FLD (rbp));
353 GC_push_one(info . THREAD_FLD (rsp));
354 GC_push_one(info . THREAD_FLD (r8));
355 GC_push_one(info . THREAD_FLD (r9));
356 GC_push_one(info . THREAD_FLD (r10));
357 GC_push_one(info . THREAD_FLD (r11));
358 GC_push_one(info . THREAD_FLD (r12));
359 GC_push_one(info . THREAD_FLD (r13));
360 GC_push_one(info . THREAD_FLD (r14));
361 GC_push_one(info . THREAD_FLD (r15));
362 GC_push_one(info . THREAD_FLD (rip));
363 GC_push_one(info . THREAD_FLD (rflags));
364 GC_push_one(info . THREAD_FLD (cs));
365 GC_push_one(info . THREAD_FLD (fs));
366 GC_push_one(info . THREAD_FLD (gs));
368 # elif defined(ARM32)
369 GC_THREAD_STATE_T info;
370 mach_msg_type_number_t outCount = THREAD_STATE_MAX;
371 r = thread_get_state(thread, GC_MACH_THREAD_STATE, (natural_t *)&info,
373 if(r != KERN_SUCCESS)
374 ABORT("task_get_state failed");
376 hi = (ptr_t)FindTopOfStack(info . __sp);
378 lo = (void*)info.__sp;
380 GC_push_one(info.__r[0]);
381 GC_push_one(info.__r[1]);
382 GC_push_one(info.__r[2]);
383 GC_push_one(info.__r[3]);
384 GC_push_one(info.__r[4]);
385 GC_push_one(info.__r[5]);
386 GC_push_one(info.__r[6]);
387 GC_push_one(info.__r[7]);
388 GC_push_one(info.__r[8]);
389 GC_push_one(info.__r[9]);
390 GC_push_one(info.__r[10]);
391 GC_push_one(info.__r[11]);
392 GC_push_one(info.__r[12]);
393 /* GC_push_one(info.__sp); */
394 GC_push_one(info.__lr);
395 GC_push_one(info.__pc);
396 GC_push_one(info.__cpsr);
399 # error FIXME for non-x86 || ppc || arm architectures
402 # ifdef DEBUG_THREADS
403 GC_printf("Darwin: Stack for thread 0x%lx = [%p,%p)\n",
404 (unsigned long) thread, lo, hi);
406 /* FIXME: use GC_push_all_stack_frames. */
407 GC_push_all_stack(lo, hi);
408 mach_port_deallocate(my_task, thread);
409 total_size += hi - lo; /* lo <= hi */
410 } /* for(i=0; ...) */
411 vm_deallocate(my_task, (vm_address_t)act_list,
412 sizeof(thread_t) * listcount);
413 mach_port_deallocate(my_task, me);
414 GC_total_stacksize = total_size;
416 #endif /* !DARWIN_DONT_PARSE_STACK */
418 STATIC mach_port_t GC_mach_handler_thread = 0;
419 STATIC int GC_use_mach_handler_thread = 0;
421 static struct GC_mach_thread GC_mach_threads[THREAD_TABLE_SZ];
422 STATIC int GC_mach_threads_count = 0;
424 GC_INNER void GC_stop_init(void)
428 for (i = 0; i < THREAD_TABLE_SZ; i++) {
429 GC_mach_threads[i].thread = 0;
430 GC_mach_threads[i].already_suspended = 0;
432 GC_mach_threads_count = 0;
435 /* returns true if there's a thread in act_list that wasn't in old_list */
436 STATIC int GC_suspend_thread_list(thread_act_array_t act_list, int count,
437 thread_act_array_t old_list, int old_count)
439 mach_port_t my_thread = mach_thread_self();
444 for(i = 0; i < count; i++) {
445 thread_act_t thread = act_list[i];
446 # ifdef DEBUG_THREADS
447 GC_printf("Attempting to suspend thread %p\n", thread);
449 /* find the current thread in the old list */
451 for(j = 0; j < old_count; j++) {
452 thread_act_t old_thread = old_list[j];
453 if (old_thread == thread) {
459 /* add it to the GC_mach_threads list */
460 GC_mach_threads[GC_mach_threads_count].thread = thread;
461 /* default is not suspended */
462 GC_mach_threads[GC_mach_threads_count].already_suspended = 0;
466 if (thread != my_thread
467 && (!GC_use_mach_handler_thread
468 || (GC_use_mach_handler_thread
469 && GC_mach_handler_thread != thread))) {
470 struct thread_basic_info info;
471 mach_msg_type_number_t outCount = THREAD_INFO_MAX;
472 kern_return_t kern_result = thread_info(thread, THREAD_BASIC_INFO,
473 (thread_info_t)&info, &outCount);
474 if(kern_result != KERN_SUCCESS) {
475 /* the thread may have quit since the thread_threads () call
476 * we mark already_suspended so it's not dealt with anymore later
479 GC_mach_threads[GC_mach_threads_count].already_suspended = TRUE;
480 GC_mach_threads_count++;
484 # ifdef DEBUG_THREADS
485 GC_printf("Thread state for 0x%lx = %d\n", (unsigned long)thread,
489 GC_mach_threads[GC_mach_threads_count].already_suspended
490 = info.suspend_count;
492 if (info.suspend_count)
495 # ifdef DEBUG_THREADS
496 GC_printf("Suspending 0x%lx\n", (unsigned long)thread);
498 /* Suspend the thread */
499 kern_result = thread_suspend(thread);
500 if(kern_result != KERN_SUCCESS) {
501 /* the thread may have quit since the thread_threads () call
502 * we mark already_suspended so it's not dealt with anymore later
505 GC_mach_threads[GC_mach_threads_count].already_suspended = TRUE;
506 GC_mach_threads_count++;
511 if (!found) GC_mach_threads_count++;
513 mach_port_deallocate(current_task(), my_thread);
518 GC_INNER void GC_mprotect_stop(void);
519 GC_INNER void GC_mprotect_resume(void);
522 /* Caller holds allocation lock. */
523 GC_INNER void GC_stop_world(void)
525 unsigned int i, changes;
526 task_t my_task = current_task();
527 mach_port_t my_thread = mach_thread_self();
528 kern_return_t kern_result;
529 thread_act_array_t act_list, prev_list;
530 mach_msg_type_number_t listcount, prevcount;
532 # ifdef DEBUG_THREADS
533 GC_printf("Stopping the world from 0x%lx\n",
534 (unsigned long)mach_thread_self());
537 /* clear out the mach threads list table */
540 /* Make sure all free list construction has stopped before we start. */
541 /* No new construction can start, since free list construction is */
542 /* required to acquire and release the GC lock before it starts, */
543 /* and we have the lock. */
544 # ifdef PARALLEL_MARK
546 GC_acquire_mark_lock();
547 GC_ASSERT(GC_fl_builder_count == 0);
548 /* We should have previously waited for it to become zero. */
550 # endif /* PARALLEL_MARK */
552 /* Loop stopping threads until you have gone over the whole list
553 twice without a new one appearing. thread_create() won't
554 return (and thus the thread stop) until the new thread
555 exists, so there is no window whereby you could stop a
556 thread, recognise it is stopped, but then have a new thread
557 it created before stopping show up later.
560 /* FIXME: This seems to erroneously stop the parallel marker threads? */
567 kern_result = task_threads(my_task, &act_list, &listcount);
569 if(kern_result == KERN_SUCCESS) {
570 result = GC_suspend_thread_list(act_list, listcount, prev_list,
574 if(prev_list != NULL) {
575 for(i = 0; i < prevcount; i++)
576 mach_port_deallocate(my_task, prev_list[i]);
578 vm_deallocate(my_task, (vm_address_t)prev_list,
579 sizeof(thread_t) * prevcount);
581 prev_list = act_list;
582 prevcount = listcount;
585 GC_ASSERT(prev_list != 0);
586 for(i = 0; i < prevcount; i++)
587 mach_port_deallocate(my_task, prev_list[i]);
589 vm_deallocate(my_task, (vm_address_t)act_list,
590 sizeof(thread_t) * listcount);
598 # ifdef PARALLEL_MARK
600 GC_release_mark_lock();
602 # ifdef DEBUG_THREADS
603 GC_printf("World stopped from 0x%lx\n", (unsigned long)my_thread);
606 mach_port_deallocate(my_task, my_thread);
609 /* Caller holds allocation lock, and has held it continuously since */
610 /* the world stopped. */
611 GC_INNER void GC_start_world(void)
613 task_t my_task = current_task();
614 mach_port_t my_thread = mach_thread_self();
617 kern_return_t kern_result;
618 thread_act_array_t act_list;
619 mach_msg_type_number_t listcount;
620 struct thread_basic_info info;
621 mach_msg_type_number_t outCount = THREAD_INFO_MAX;
623 # ifdef DEBUG_THREADS
624 GC_printf("World starting\n");
629 GC_mprotect_resume();
633 kern_result = task_threads(my_task, &act_list, &listcount);
634 for(i = 0; i < listcount; i++) {
635 thread_act_t thread = act_list[i];
636 if (thread != my_thread
637 && (!GC_use_mach_handler_thread
638 || (GC_use_mach_handler_thread
639 && GC_mach_handler_thread != thread))) {
640 for(j = 0; j < GC_mach_threads_count; j++) {
641 if (thread == GC_mach_threads[j].thread) {
642 if (GC_mach_threads[j].already_suspended) {
643 # ifdef DEBUG_THREADS
644 GC_printf("Not resuming already suspended thread %p\n", thread);
648 kern_result = thread_info(thread, THREAD_BASIC_INFO,
649 (thread_info_t)&info, &outCount);
650 if(kern_result != KERN_SUCCESS)
651 ABORT("thread_info failed");
652 # ifdef DEBUG_THREADS
653 GC_printf("Thread state for 0x%lx = %d\n", (unsigned long)thread,
655 GC_printf("Resuming 0x%lx\n", (unsigned long)thread);
657 /* Resume the thread */
658 kern_result = thread_resume(thread);
659 if(kern_result != KERN_SUCCESS)
660 ABORT("thread_resume failed");
664 mach_port_deallocate(my_task, thread);
666 vm_deallocate(my_task, (vm_address_t)act_list,
667 sizeof(thread_t) * listcount);
669 mach_port_deallocate(my_task, my_thread);
670 # ifdef DEBUG_THREADS
671 GC_printf("World started\n");
675 GC_INNER void GC_darwin_register_mach_handler_thread(mach_port_t thread)
677 GC_mach_handler_thread = thread;
678 GC_use_mach_handler_thread = 1;