1 #include "private/pthread_support.h"
3 # if defined(GC_DARWIN_THREADS)
5 #include <AvailabilityMacros.h>
6 #include "mono/utils/mono-compiler.h"
8 #ifdef MONO_DEBUGGER_SUPPORTED
9 #include "include/libgc-mono-debugger.h"
12 /* From "Inside Mac OS X - Mach-O Runtime Architecture" published by Apple
14 "The space beneath the stack pointer, where a new stack frame would normally
15 be allocated, is called the red zone. This area as shown in Figure 3-2 may
16 be used for any purpose as long as a new stack frame does not need to be
19 Page 50: "If a leaf procedure's red zone usage would exceed 224 bytes, then
20 it must set up a stack frame just like routines that call other routines."
24 # define PPC_RED_ZONE_SIZE 224
25 # elif CPP_WORDSZ == 64
26 # define PPC_RED_ZONE_SIZE 320
30 typedef struct StackFrame
{
31 unsigned long savedSP
;
32 unsigned long savedCR
;
33 unsigned long savedLR
;
34 unsigned long reserved
[2];
35 unsigned long savedRTOC
;
38 unsigned long FindTopOfStack(unsigned int stack_start
) {
41 if (stack_start
== 0) {
44 __asm__
volatile("lwz %0,0(r1)" : "=r" (frame
));
46 __asm__
volatile("ldz %0,0(r1)" : "=r" (frame
));
50 frame
= (StackFrame
*)stack_start
;
54 /* GC_printf1("FindTopOfStack start at sp = %p\n", frame); */
57 if (frame
->savedSP
== 0) break;
58 /* if there are no more stack frames, stop */
60 frame
= (StackFrame
*)frame
->savedSP
;
62 /* we do these next two checks after going to the next frame
63 because the LR for the first stack frame in the loop
64 is not set up on purpose, so we shouldn't check it. */
65 if ((frame
->savedLR
& ~3) == 0) break; /* if the next LR is bogus, stop */
66 if ((~(frame
->savedLR
) & ~3) == 0) break; /* ditto */
70 /* GC_printf1("FindTopOfStack finish at sp = %p\n", frame); */
73 return (unsigned long)frame
;
76 #ifdef DARWIN_DONT_PARSE_STACK
77 void GC_push_all_stacks() {
84 ppc_thread_state_t state
;
85 mach_msg_type_number_t thread_state_count
= PPC_THREAD_STATE_COUNT
;
87 i386_thread_state_t state
;
88 mach_msg_type_number_t thread_state_count
= i386_THREAD_STATE_COUNT
;
90 arm_thread_state_t state
;
91 mach_msg_type_number_t thread_state_count
= ARM_THREAD_STATE_COUNT
;
93 # error FIXME for non-x86 || ppc architectures
94 mach_msg_type_number_t thread_state_count
= MACHINE_THREAD_STATE_COUNT
;
98 if (!GC_thr_initialized
) GC_thr_init();
100 for(i
=0;i
<THREAD_TABLE_SZ
;i
++) {
101 for(p
=GC_threads
[i
];p
!=0;p
=p
->next
) {
102 if(p
-> flags
& FINISHED
) continue;
103 if(pthread_equal(p
->id
,me
)) {
106 /* Get the thread state (registers, etc) */
107 r
= thread_get_state(
108 p
->stop_info
.mach_thread
,
109 GC_MACH_THREAD_STATE_FLAVOR
,
111 &thread_state_count
);
112 if(r
!= KERN_SUCCESS
) ABORT("thread_get_state failed");
115 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
119 GC_push_one(state
.__eax
);
120 GC_push_one(state
.__ebx
);
121 GC_push_one(state
.__ecx
);
122 GC_push_one(state
.__edx
);
123 GC_push_one(state
.__edi
);
124 GC_push_one(state
.__esi
);
125 GC_push_one(state
.__ebp
);
129 GC_push_one(state
.eax
);
130 GC_push_one(state
.ebx
);
131 GC_push_one(state
.ecx
);
132 GC_push_one(state
.edx
);
133 GC_push_one(state
.edi
);
134 GC_push_one(state
.esi
);
135 GC_push_one(state
.ebp
);
137 #elif defined(POWERPC)
138 #if defined(_STRUCT_PPC_EXCEPTION_STATE)
139 lo
= (void*)(state
.__r1
- PPC_RED_ZONE_SIZE
);
141 GC_push_one(state
.__r0
);
142 GC_push_one(state
.__r2
);
143 GC_push_one(state
.__r3
);
144 GC_push_one(state
.__r4
);
145 GC_push_one(state
.__r5
);
146 GC_push_one(state
.__r6
);
147 GC_push_one(state
.__r7
);
148 GC_push_one(state
.__r8
);
149 GC_push_one(state
.__r9
);
150 GC_push_one(state
.__r10
);
151 GC_push_one(state
.__r11
);
152 GC_push_one(state
.__r12
);
153 GC_push_one(state
.__r13
);
154 GC_push_one(state
.__r14
);
155 GC_push_one(state
.__r15
);
156 GC_push_one(state
.__r16
);
157 GC_push_one(state
.__r17
);
158 GC_push_one(state
.__r18
);
159 GC_push_one(state
.__r19
);
160 GC_push_one(state
.__r20
);
161 GC_push_one(state
.__r21
);
162 GC_push_one(state
.__r22
);
163 GC_push_one(state
.__r23
);
164 GC_push_one(state
.__r24
);
165 GC_push_one(state
.__r25
);
166 GC_push_one(state
.__r26
);
167 GC_push_one(state
.__r27
);
168 GC_push_one(state
.__r28
);
169 GC_push_one(state
.__r29
);
170 GC_push_one(state
.__r30
);
171 GC_push_one(state
.__r31
);
173 lo
= (void*)(state
.r1
- PPC_RED_ZONE_SIZE
);
175 GC_push_one(state
.r0
);
176 GC_push_one(state
.r2
);
177 GC_push_one(state
.r3
);
178 GC_push_one(state
.r4
);
179 GC_push_one(state
.r5
);
180 GC_push_one(state
.r6
);
181 GC_push_one(state
.r7
);
182 GC_push_one(state
.r8
);
183 GC_push_one(state
.r9
);
184 GC_push_one(state
.r10
);
185 GC_push_one(state
.r11
);
186 GC_push_one(state
.r12
);
187 GC_push_one(state
.r13
);
188 GC_push_one(state
.r14
);
189 GC_push_one(state
.r15
);
190 GC_push_one(state
.r16
);
191 GC_push_one(state
.r17
);
192 GC_push_one(state
.r18
);
193 GC_push_one(state
.r19
);
194 GC_push_one(state
.r20
);
195 GC_push_one(state
.r21
);
196 GC_push_one(state
.r22
);
197 GC_push_one(state
.r23
);
198 GC_push_one(state
.r24
);
199 GC_push_one(state
.r25
);
200 GC_push_one(state
.r26
);
201 GC_push_one(state
.r27
);
202 GC_push_one(state
.r28
);
203 GC_push_one(state
.r29
);
204 GC_push_one(state
.r30
);
205 GC_push_one(state
.r31
);
208 lo
= (void*)state
.__sp
;
210 GC_push_one(state
.__r
[0]);
211 GC_push_one(state
.__r
[1]);
212 GC_push_one(state
.__r
[2]);
213 GC_push_one(state
.__r
[3]);
214 GC_push_one(state
.__r
[4]);
215 GC_push_one(state
.__r
[5]);
216 GC_push_one(state
.__r
[6]);
217 GC_push_one(state
.__r
[7]);
218 GC_push_one(state
.__r
[8]);
219 GC_push_one(state
.__r
[9]);
220 GC_push_one(state
.__r
[10]);
221 GC_push_one(state
.__r
[11]);
222 GC_push_one(state
.__r
[12]);
223 /* GC_push_one(state.__sp); */
224 GC_push_one(state
.__lr
);
225 GC_push_one(state
.__pc
);
226 GC_push_one(state
.__cpsr
);
228 # error FIXME for non-x86 || ppc architectures
231 if(p
->flags
& MAIN_THREAD
)
236 GC_printf3("Darwin: Stack for thread 0x%lx = [%lx,%lx)\n",
237 (unsigned long) p
-> id
,
242 GC_push_all_stack(lo
,hi
);
243 } /* for(p=GC_threads[i]...) */
244 } /* for(i=0;i<THREAD_TABLE_SZ...) */
247 #else /* !DARWIN_DONT_PARSE_STACK; Use FindTopOfStack() */
249 void GC_push_all_stacks() {
255 thread_act_array_t act_list
= 0;
256 mach_msg_type_number_t listcount
= 0;
258 me
= mach_thread_self();
259 if (!GC_thr_initialized
) GC_thr_init();
261 my_task
= current_task();
262 r
= task_threads(my_task
, &act_list
, &listcount
);
263 if(r
!= KERN_SUCCESS
) ABORT("task_threads failed");
264 for(i
= 0; i
< listcount
; i
++) {
265 thread_act_t thread
= act_list
[i
];
268 hi
= (ptr_t
)FindTopOfStack(0);
270 # if defined(POWERPC)
271 # if CPP_WORDSZ == 32
272 ppc_thread_state_t info
;
274 ppc_thread_state64_t info
;
276 mach_msg_type_number_t outCount
= THREAD_STATE_MAX
;
277 r
= thread_get_state(thread
, GC_MACH_THREAD_STATE_FLAVOR
,
278 (natural_t
*)&info
, &outCount
);
279 if(r
!= KERN_SUCCESS
) continue;
281 #if defined(_STRUCT_PPC_EXCEPTION_STATE)
282 lo
= (void*)(info
.__r1
- PPC_RED_ZONE_SIZE
);
283 hi
= (ptr_t
)FindTopOfStack(info
.__r1
);
285 GC_push_one(info
.__r0
);
286 GC_push_one(info
.__r2
);
287 GC_push_one(info
.__r3
);
288 GC_push_one(info
.__r4
);
289 GC_push_one(info
.__r5
);
290 GC_push_one(info
.__r6
);
291 GC_push_one(info
.__r7
);
292 GC_push_one(info
.__r8
);
293 GC_push_one(info
.__r9
);
294 GC_push_one(info
.__r10
);
295 GC_push_one(info
.__r11
);
296 GC_push_one(info
.__r12
);
297 GC_push_one(info
.__r13
);
298 GC_push_one(info
.__r14
);
299 GC_push_one(info
.__r15
);
300 GC_push_one(info
.__r16
);
301 GC_push_one(info
.__r17
);
302 GC_push_one(info
.__r18
);
303 GC_push_one(info
.__r19
);
304 GC_push_one(info
.__r20
);
305 GC_push_one(info
.__r21
);
306 GC_push_one(info
.__r22
);
307 GC_push_one(info
.__r23
);
308 GC_push_one(info
.__r24
);
309 GC_push_one(info
.__r25
);
310 GC_push_one(info
.__r26
);
311 GC_push_one(info
.__r27
);
312 GC_push_one(info
.__r28
);
313 GC_push_one(info
.__r29
);
314 GC_push_one(info
.__r30
);
315 GC_push_one(info
.__r31
);
317 lo
= (void*)(info
.r1
- PPC_RED_ZONE_SIZE
);
318 hi
= (ptr_t
)FindTopOfStack(info
.r1
);
320 GC_push_one(info
.r0
);
321 GC_push_one(info
.r2
);
322 GC_push_one(info
.r3
);
323 GC_push_one(info
.r4
);
324 GC_push_one(info
.r5
);
325 GC_push_one(info
.r6
);
326 GC_push_one(info
.r7
);
327 GC_push_one(info
.r8
);
328 GC_push_one(info
.r9
);
329 GC_push_one(info
.r10
);
330 GC_push_one(info
.r11
);
331 GC_push_one(info
.r12
);
332 GC_push_one(info
.r13
);
333 GC_push_one(info
.r14
);
334 GC_push_one(info
.r15
);
335 GC_push_one(info
.r16
);
336 GC_push_one(info
.r17
);
337 GC_push_one(info
.r18
);
338 GC_push_one(info
.r19
);
339 GC_push_one(info
.r20
);
340 GC_push_one(info
.r21
);
341 GC_push_one(info
.r22
);
342 GC_push_one(info
.r23
);
343 GC_push_one(info
.r24
);
344 GC_push_one(info
.r25
);
345 GC_push_one(info
.r26
);
346 GC_push_one(info
.r27
);
347 GC_push_one(info
.r28
);
348 GC_push_one(info
.r29
);
349 GC_push_one(info
.r30
);
350 GC_push_one(info
.r31
);
352 # elif defined(I386) /* !POWERPC */
353 /* FIXME: Remove after testing: */
354 WARN("This is completely untested and likely will not work\n", 0);
355 i386_thread_state_t info
;
356 mach_msg_type_number_t outCount
= THREAD_STATE_MAX
;
357 r
= thread_get_state(thread
, GC_MACH_THREAD_STATE_FLAVOR
,
358 (natural_t
*)&info
, &outCount
);
359 if(r
!= KERN_SUCCESS
) continue;
361 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
362 lo
= (void*)info
.__esp
;
363 hi
= (ptr_t
)FindTopOfStack(info
.__esp
);
365 GC_push_one(info
.__eax
);
366 GC_push_one(info
.__ebx
);
367 GC_push_one(info
.__ecx
);
368 GC_push_one(info
.__edx
);
369 GC_push_one(info
.__edi
);
370 GC_push_one(info
.__esi
);
371 GC_push_one(info
.__ebp
);
372 /* GC_push_one(info.__esp); */
373 GC_push_one(info
.__ss
);
374 GC_push_one(info
.__eip
);
375 GC_push_one(info
.__cs
);
376 GC_push_one(info
.__ds
);
377 GC_push_one(info
.__es
);
378 GC_push_one(info
.__fs
);
379 GC_push_one(info
.__gs
);
381 lo
= (void*)info
.esp
;
382 hi
= (ptr_t
)FindTopOfStack(info
.esp
);
384 GC_push_one(info
.eax
);
385 GC_push_one(info
.ebx
);
386 GC_push_one(info
.ecx
);
387 GC_push_one(info
.edx
);
388 GC_push_one(info
.edi
);
389 GC_push_one(info
.esi
);
390 GC_push_one(info
.ebp
);
391 /* GC_push_one(info.esp); */
392 GC_push_one(info
.ss
);
393 GC_push_one(info
.eip
);
394 GC_push_one(info
.cs
);
395 GC_push_one(info
.ds
);
396 GC_push_one(info
.es
);
397 GC_push_one(info
.fs
);
398 GC_push_one(info
.gs
);
400 # elif defined(ARM) /* !I386 */
401 arm_thread_state_t info
;
402 mach_msg_type_number_t outCount
= THREAD_STATE_MAX
;
403 r
= thread_get_state(thread
, GC_MACH_THREAD_STATE_FLAVOR
,
404 (natural_t
*)&info
, &outCount
);
405 if(r
!= KERN_SUCCESS
) continue;
407 lo
= (void*)info
.__sp
;
408 hi
= (ptr_t
)FindTopOfStack(info
.__sp
);
410 GC_push_one(info
.__r
[0]);
411 GC_push_one(info
.__r
[1]);
412 GC_push_one(info
.__r
[2]);
413 GC_push_one(info
.__r
[3]);
414 GC_push_one(info
.__r
[4]);
415 GC_push_one(info
.__r
[5]);
416 GC_push_one(info
.__r
[6]);
417 GC_push_one(info
.__r
[7]);
418 GC_push_one(info
.__r
[8]);
419 GC_push_one(info
.__r
[9]);
420 GC_push_one(info
.__r
[10]);
421 GC_push_one(info
.__r
[11]);
422 GC_push_one(info
.__r
[12]);
423 /* GC_push_one(info.__sp); */
424 GC_push_one(info
.__lr
);
425 GC_push_one(info
.__pc
);
426 GC_push_one(info
.__cpsr
);
430 GC_printf3("Darwin: Stack for thread 0x%lx = [%lx,%lx)\n",
431 (unsigned long) thread
,
436 GC_push_all_stack(lo
, hi
);
437 mach_port_deallocate(my_task
, thread
);
438 } /* for(p=GC_threads[i]...) */
439 vm_deallocate(my_task
, (vm_address_t
)act_list
, sizeof(thread_t
) * listcount
);
440 mach_port_deallocate(my_task
, me
);
442 #endif /* !DARWIN_DONT_PARSE_STACK */
444 static mach_port_t GC_mach_handler_thread
;
445 static int GC_use_mach_handler_thread
= 0;
447 #define SUSPEND_THREADS_SIZE 2048
448 static struct GC_mach_thread GC_mach_threads
[SUSPEND_THREADS_SIZE
];
449 static int GC_mach_threads_count
;
451 void GC_stop_init() {
454 for (i
= 0; i
< SUSPEND_THREADS_SIZE
; i
++) {
455 GC_mach_threads
[i
].thread
= 0;
456 GC_mach_threads
[i
].already_suspended
= 0;
458 GC_mach_threads_count
= 0;
461 /* returns true if there's a thread in act_list that wasn't in old_list */
462 int GC_suspend_thread_list(thread_act_array_t act_list
, int count
,
463 thread_act_array_t old_list
, int old_count
) {
464 mach_port_t my_thread
= mach_thread_self();
469 for(i
= 0; i
< count
; i
++) {
470 thread_act_t thread
= act_list
[i
];
472 GC_printf1("Attempting to suspend thread %p\n", thread
);
474 /* find the current thread in the old list */
476 for(j
= 0; j
< old_count
; j
++) {
477 thread_act_t old_thread
= old_list
[j
];
478 if (old_thread
== thread
) {
484 /* add it to the GC_mach_threads list */
485 GC_mach_threads
[GC_mach_threads_count
].thread
= thread
;
486 /* default is not suspended */
487 GC_mach_threads
[GC_mach_threads_count
].already_suspended
= 0;
491 if (thread
!= my_thread
&&
492 (!GC_use_mach_handler_thread
493 || (GC_use_mach_handler_thread
494 && GC_mach_handler_thread
!= thread
))) {
495 struct thread_basic_info info
;
496 mach_msg_type_number_t outCount
= THREAD_INFO_MAX
;
497 kern_return_t kern_result
= thread_info(thread
, THREAD_BASIC_INFO
,
498 (thread_info_t
)&info
, &outCount
);
499 if(kern_result
!= KERN_SUCCESS
) {
500 /* the thread may have quit since the thread_threads () call
501 * we mark already_suspended so it's not dealt with anymore later
504 GC_mach_threads
[GC_mach_threads_count
].already_suspended
= TRUE
;
505 GC_mach_threads_count
++;
510 GC_printf2("Thread state for 0x%lx = %d\n", thread
, info
.run_state
);
513 GC_mach_threads
[GC_mach_threads_count
].already_suspended
= info
.suspend_count
;
515 if (info
.suspend_count
) continue;
518 GC_printf1("Suspending 0x%lx\n", thread
);
520 /* Suspend the thread */
521 kern_result
= thread_suspend(thread
);
522 if(kern_result
!= KERN_SUCCESS
) {
523 /* the thread may have quit since the thread_threads () call
524 * we mark already_suspended so it's not dealt with anymore later
527 GC_mach_threads
[GC_mach_threads_count
].already_suspended
= TRUE
;
528 GC_mach_threads_count
++;
533 if (!found
) GC_mach_threads_count
++;
536 mach_port_deallocate(current_task(), my_thread
);
541 /* Caller holds allocation lock. */
546 task_t my_task
= current_task();
547 mach_port_t my_thread
= mach_thread_self();
548 kern_return_t kern_result
;
549 thread_act_array_t act_list
, prev_list
;
550 mach_msg_type_number_t listcount
, prevcount
;
553 GC_printf1("Stopping the world from 0x%lx\n", mach_thread_self());
556 /* clear out the mach threads list table */
559 /* Make sure all free list construction has stopped before we start. */
560 /* No new construction can start, since free list construction is */
561 /* required to acquire and release the GC lock before it starts, */
562 /* and we have the lock. */
563 # ifdef PARALLEL_MARK
564 GC_acquire_mark_lock();
565 GC_ASSERT(GC_fl_builder_count
== 0);
566 /* We should have previously waited for it to become zero. */
567 # endif /* PARALLEL_MARK */
569 /* Loop stopping threads until you have gone over the whole list
570 twice without a new one appearing. thread_create() won't
571 return (and thus the thread stop) until the new thread
572 exists, so there is no window whereby you could stop a
573 thread, recognise it is stopped, but then have a new thread
574 it created before stopping show up later.
582 kern_result
= task_threads(my_task
, &act_list
, &listcount
);
584 if(kern_result
== KERN_SUCCESS
) {
585 result
= GC_suspend_thread_list(act_list
, listcount
,
586 prev_list
, prevcount
);
589 if(prev_list
!= NULL
) {
590 for(i
= 0; i
< prevcount
; i
++)
591 mach_port_deallocate(my_task
, prev_list
[i
]);
593 vm_deallocate(my_task
, (vm_address_t
)prev_list
, sizeof(thread_t
) * prevcount
);
596 prev_list
= act_list
;
597 prevcount
= listcount
;
601 for(i
= 0; i
< listcount
; i
++)
602 mach_port_deallocate(my_task
, act_list
[i
]);
604 vm_deallocate(my_task
, (vm_address_t
)act_list
, sizeof(thread_t
) * listcount
);
609 extern void GC_mprotect_stop();
614 # ifdef PARALLEL_MARK
615 GC_release_mark_lock();
618 GC_printf1("World stopped from 0x%lx\n", my_thread
);
621 mach_port_deallocate(my_task
, my_thread
);
624 /* Caller holds allocation lock, and has held it continuously since */
625 /* the world stopped. */
626 void GC_start_world()
628 task_t my_task
= current_task();
629 mach_port_t my_thread
= mach_thread_self();
632 kern_return_t kern_result
;
633 thread_act_array_t act_list
;
634 mach_msg_type_number_t listcount
;
635 struct thread_basic_info info
;
636 mach_msg_type_number_t outCount
= THREAD_INFO_MAX
;
639 GC_printf0("World starting\n");
644 extern void GC_mprotect_resume();
645 GC_mprotect_resume();
649 kern_result
= task_threads(my_task
, &act_list
, &listcount
);
650 for(i
= 0; i
< listcount
; i
++) {
651 thread_act_t thread
= act_list
[i
];
652 if (thread
!= my_thread
&&
653 (!GC_use_mach_handler_thread
||
654 (GC_use_mach_handler_thread
&& GC_mach_handler_thread
!= thread
))) {
655 for(j
= 0; j
< GC_mach_threads_count
; j
++) {
656 if (thread
== GC_mach_threads
[j
].thread
) {
657 if (GC_mach_threads
[j
].already_suspended
) {
659 GC_printf1("Not resuming already suspended thread %p\n", thread
);
663 kern_result
= thread_info(thread
, THREAD_BASIC_INFO
,
664 (thread_info_t
)&info
, &outCount
);
665 if(kern_result
!= KERN_SUCCESS
) continue;
667 GC_printf2("Thread state for 0x%lx = %d\n", thread
,
669 GC_printf1("Resuming 0x%lx\n", thread
);
671 /* Resume the thread */
672 kern_result
= thread_resume(thread
);
673 if(kern_result
!= KERN_SUCCESS
) continue;
678 mach_port_deallocate(my_task
, thread
);
680 vm_deallocate(my_task
, (vm_address_t
)act_list
, sizeof(thread_t
) * listcount
);
682 mach_port_deallocate(my_task
, my_thread
);
684 GC_printf0("World started\n");
688 void GC_darwin_register_mach_handler_thread(mach_port_t thread
) {
689 GC_mach_handler_thread
= thread
;
690 GC_use_mach_handler_thread
= 1;
693 #ifdef MONO_DEBUGGER_SUPPORTED
694 GCThreadFunctions
*gc_thread_vtable
= NULL
;
697 GC_mono_debugger_get_stack_ptr (void)
701 me
= GC_lookup_thread (pthread_self ());
702 return &me
->stop_info
.stack_ptr
;