* ipa-reference.c (analyze_function): Consider also addresses taken
[official-gcc.git] / boehm-gc / darwin_stop_world.c
blob5ddb271646ee76972a2b68d99fc9da6c00389e5b
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
8 Page 49:
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
12 added to the stack."
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."
17 #if defined(__ppc__)
18 # define PPC_RED_ZONE_SIZE 224
19 #elif defined(__ppc64__)
20 # define PPC_RED_ZONE_SIZE 320
21 #endif
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. */
25 /* This now defaults to the (older) names without __, thus hopefully */
26 /* not breaking any existing Makefile.direct builds. */
27 #if defined (HAS_PPC_THREAD_STATE___R0) || \
28 defined (HAS_PPC_THREAD_STATE64___R0) || \
29 defined (HAS_X86_THREAD_STATE32___EAX) || \
30 defined (HAS_X86_THREAD_STATE64___RAX)
31 # define THREAD_FLD(x) __ ## x
32 #else
33 # define THREAD_FLD(x) x
34 #endif
36 typedef struct StackFrame {
37 unsigned long savedSP;
38 unsigned long savedCR;
39 unsigned long savedLR;
40 unsigned long reserved[2];
41 unsigned long savedRTOC;
42 } StackFrame;
44 unsigned long FindTopOfStack(unsigned long stack_start) {
45 StackFrame *frame;
47 if (stack_start == 0) {
48 # ifdef POWERPC
49 # if CPP_WORDSZ == 32
50 __asm__ volatile("lwz %0,0(r1)" : "=r" (frame));
51 # else
52 __asm__ volatile("ld %0,0(r1)" : "=r" (frame));
53 # endif
54 # endif
55 } else {
56 frame = (StackFrame *)stack_start;
59 # ifdef DEBUG_THREADS
60 /* GC_printf1("FindTopOfStack start at sp = %p\n", frame); */
61 # endif
62 do {
63 if (frame->savedSP == 0) break;
64 /* if there are no more stack frames, stop */
66 frame = (StackFrame*)frame->savedSP;
68 /* we do these next two checks after going to the next frame
69 because the LR for the first stack frame in the loop
70 is not set up on purpose, so we shouldn't check it. */
71 if ((frame->savedLR & ~3) == 0) break; /* if the next LR is bogus, stop */
72 if ((~(frame->savedLR) & ~3) == 0) break; /* ditto */
73 } while (1);
75 # ifdef DEBUG_THREADS
76 /* GC_printf1("FindTopOfStack finish at sp = %p\n", frame); */
77 # endif
79 return (unsigned long)frame;
82 #ifdef DARWIN_DONT_PARSE_STACK
83 void GC_push_all_stacks() {
84 int i;
85 kern_return_t r;
86 GC_thread p;
87 pthread_t me;
88 ptr_t lo, hi;
89 GC_THREAD_STATE_T state;
90 mach_msg_type_number_t thread_state_count = GC_MACH_THREAD_STATE_COUNT;
92 me = pthread_self();
93 if (!GC_thr_initialized) GC_thr_init();
95 for(i=0;i<THREAD_TABLE_SZ;i++) {
96 for(p=GC_threads[i];p!=0;p=p->next) {
97 if(p -> flags & FINISHED) continue;
98 if(pthread_equal(p->id,me)) {
99 lo = GC_approx_sp();
100 } else {
101 /* Get the thread state (registers, etc) */
102 r = thread_get_state(p->stop_info.mach_thread, GC_MACH_THREAD_STATE,
103 (natural_t*)&state, &thread_state_count);
104 if(r != KERN_SUCCESS) ABORT("thread_get_state failed");
106 #if defined(I386)
107 lo = (void*)state . THREAD_FLD (esp);
109 GC_push_one(state . THREAD_FLD (eax));
110 GC_push_one(state . THREAD_FLD (ebx));
111 GC_push_one(state . THREAD_FLD (ecx));
112 GC_push_one(state . THREAD_FLD (edx));
113 GC_push_one(state . THREAD_FLD (edi));
114 GC_push_one(state . THREAD_FLD (esi));
115 GC_push_one(state . THREAD_FLD (ebp));
117 #elif defined(X86_64)
118 lo = (void*)state . THREAD_FLD (rsp);
120 GC_push_one(state . THREAD_FLD (rax));
121 GC_push_one(state . THREAD_FLD (rbx));
122 GC_push_one(state . THREAD_FLD (rcx));
123 GC_push_one(state . THREAD_FLD (rdx));
124 GC_push_one(state . THREAD_FLD (rdi));
125 GC_push_one(state . THREAD_FLD (rsi));
126 GC_push_one(state . THREAD_FLD (rbp));
127 GC_push_one(state . THREAD_FLD (rsp));
128 GC_push_one(state . THREAD_FLD (r8));
129 GC_push_one(state . THREAD_FLD (r9));
130 GC_push_one(state . THREAD_FLD (r10));
131 GC_push_one(state . THREAD_FLD (r11));
132 GC_push_one(state . THREAD_FLD (r12));
133 GC_push_one(state . THREAD_FLD (r13));
134 GC_push_one(state . THREAD_FLD (r14));
135 GC_push_one(state . THREAD_FLD (r15));
136 GC_push_one(state . THREAD_FLD (rip));
137 GC_push_one(state . THREAD_FLD (rflags));
138 GC_push_one(state . THREAD_FLD (cs));
139 GC_push_one(state . THREAD_FLD (fs));
140 GC_push_one(state . THREAD_FLD (gs));
142 #elif defined(POWERPC)
143 lo = (void*)(state . THREAD_FLD (r1) - PPC_RED_ZONE_SIZE);
145 GC_push_one(state . THREAD_FLD (r0));
146 GC_push_one(state . THREAD_FLD (r2));
147 GC_push_one(state . THREAD_FLD (r3));
148 GC_push_one(state . THREAD_FLD (r4));
149 GC_push_one(state . THREAD_FLD (r5));
150 GC_push_one(state . THREAD_FLD (r6));
151 GC_push_one(state . THREAD_FLD (r7));
152 GC_push_one(state . THREAD_FLD (r8));
153 GC_push_one(state . THREAD_FLD (r9));
154 GC_push_one(state . THREAD_FLD (r10));
155 GC_push_one(state . THREAD_FLD (r11));
156 GC_push_one(state . THREAD_FLD (r12));
157 GC_push_one(state . THREAD_FLD (r13));
158 GC_push_one(state . THREAD_FLD (r14));
159 GC_push_one(state . THREAD_FLD (r15));
160 GC_push_one(state . THREAD_FLD (r16));
161 GC_push_one(state . THREAD_FLD (r17));
162 GC_push_one(state . THREAD_FLD (r18));
163 GC_push_one(state . THREAD_FLD (r19));
164 GC_push_one(state . THREAD_FLD (r20));
165 GC_push_one(state . THREAD_FLD (r21));
166 GC_push_one(state . THREAD_FLD (r22));
167 GC_push_one(state . THREAD_FLD (r23));
168 GC_push_one(state . THREAD_FLD (r24));
169 GC_push_one(state . THREAD_FLD (r25));
170 GC_push_one(state . THREAD_FLD (r26));
171 GC_push_one(state . THREAD_FLD (r27));
172 GC_push_one(state . THREAD_FLD (r28));
173 GC_push_one(state . THREAD_FLD (r29));
174 GC_push_one(state . THREAD_FLD (r30));
175 GC_push_one(state . THREAD_FLD (r31));
176 #else
177 # error FIXME for non-x86 || ppc architectures
178 #endif
179 } /* p != me */
180 if(p->flags & MAIN_THREAD)
181 hi = GC_stackbottom;
182 else
183 hi = p->stack_end;
184 #if DEBUG_THREADS
185 GC_printf3("Darwin: Stack for thread 0x%lx = [%lx,%lx)\n",
186 (unsigned long) p -> id,
187 (unsigned long) lo,
188 (unsigned long) hi
190 #endif
191 GC_push_all_stack(lo,hi);
192 } /* for(p=GC_threads[i]...) */
193 } /* for(i=0;i<THREAD_TABLE_SZ...) */
196 #else /* !DARWIN_DONT_PARSE_STACK; Use FindTopOfStack() */
198 void GC_push_all_stacks() {
199 int i;
200 kern_return_t r;
201 mach_port_t me;
202 ptr_t lo, hi;
203 thread_act_array_t act_list = 0;
204 mach_msg_type_number_t listcount = 0;
206 me = mach_thread_self();
207 if (!GC_thr_initialized) GC_thr_init();
209 r = task_threads(current_task(), &act_list, &listcount);
210 if(r != KERN_SUCCESS) ABORT("task_threads failed");
211 for(i = 0; i < listcount; i++) {
212 thread_act_t thread = act_list[i];
213 if (thread == me) {
214 lo = GC_approx_sp();
215 hi = (ptr_t)FindTopOfStack(0);
216 } else {
217 # if defined(__ppc__) || defined(__ppc64__)
218 GC_THREAD_STATE_T info;
219 mach_msg_type_number_t outCount = THREAD_STATE_MAX;
220 r = thread_get_state(thread, GC_MACH_THREAD_STATE,
221 (natural_t *)&info, &outCount);
222 if(r != KERN_SUCCESS) ABORT("task_get_state failed");
224 lo = (void*)(info . THREAD_FLD (r1) - PPC_RED_ZONE_SIZE);
225 hi = (ptr_t)FindTopOfStack(info . THREAD_FLD (r1));
227 GC_push_one(info . THREAD_FLD (r0));
228 GC_push_one(info . THREAD_FLD (r2));
229 GC_push_one(info . THREAD_FLD (r3));
230 GC_push_one(info . THREAD_FLD (r4));
231 GC_push_one(info . THREAD_FLD (r5));
232 GC_push_one(info . THREAD_FLD (r6));
233 GC_push_one(info . THREAD_FLD (r7));
234 GC_push_one(info . THREAD_FLD (r8));
235 GC_push_one(info . THREAD_FLD (r9));
236 GC_push_one(info . THREAD_FLD (r10));
237 GC_push_one(info . THREAD_FLD (r11));
238 GC_push_one(info . THREAD_FLD (r12));
239 GC_push_one(info . THREAD_FLD (r13));
240 GC_push_one(info . THREAD_FLD (r14));
241 GC_push_one(info . THREAD_FLD (r15));
242 GC_push_one(info . THREAD_FLD (r16));
243 GC_push_one(info . THREAD_FLD (r17));
244 GC_push_one(info . THREAD_FLD (r18));
245 GC_push_one(info . THREAD_FLD (r19));
246 GC_push_one(info . THREAD_FLD (r20));
247 GC_push_one(info . THREAD_FLD (r21));
248 GC_push_one(info . THREAD_FLD (r22));
249 GC_push_one(info . THREAD_FLD (r23));
250 GC_push_one(info . THREAD_FLD (r24));
251 GC_push_one(info . THREAD_FLD (r25));
252 GC_push_one(info . THREAD_FLD (r26));
253 GC_push_one(info . THREAD_FLD (r27));
254 GC_push_one(info . THREAD_FLD (r28));
255 GC_push_one(info . THREAD_FLD (r29));
256 GC_push_one(info . THREAD_FLD (r30));
257 GC_push_one(info . THREAD_FLD (r31));
258 # else
259 /* FIXME: Remove after testing: */
260 WARN("This is completely untested and likely will not work\n", 0);
261 GC_THREAD_STATE_T info;
262 mach_msg_type_number_t outCount = THREAD_STATE_MAX;
263 r = thread_get_state(thread, GC_MACH_THREAD_STATE, (natural_t *)&info,
264 &outCount);
265 if(r != KERN_SUCCESS) ABORT("task_get_state failed");
267 lo = (void*)info . THREAD_FLD (esp);
268 hi = (ptr_t)FindTopOfStack(info . THREAD_FLD (esp));
270 GC_push_one(info . THREAD_FLD (eax));
271 GC_push_one(info . THREAD_FLD (ebx));
272 GC_push_one(info . THREAD_FLD (ecx));
273 GC_push_one(info . THREAD_FLD (edx));
274 GC_push_one(info . THREAD_FLD (edi));
275 GC_push_one(info . THREAD_FLD (esi));
276 /* GC_push_one(info . THREAD_FLD (ebp)); */
277 /* GC_push_one(info . THREAD_FLD (esp)); */
278 GC_push_one(info . THREAD_FLD (ss));
279 GC_push_one(info . THREAD_FLD (eip));
280 GC_push_one(info . THREAD_FLD (cs));
281 GC_push_one(info . THREAD_FLD (ds));
282 GC_push_one(info . THREAD_FLD (es));
283 GC_push_one(info . THREAD_FLD (fs));
284 GC_push_one(info . THREAD_FLD (gs));
285 # endif /* !POWERPC */
287 # if DEBUG_THREADS
288 GC_printf3("Darwin: Stack for thread 0x%lx = [%lx,%lx)\n",
289 (unsigned long) thread,
290 (unsigned long) lo,
291 (unsigned long) hi
293 # endif
294 GC_push_all_stack(lo, hi);
295 } /* for(p=GC_threads[i]...) */
296 vm_deallocate(current_task(), (vm_address_t)act_list, sizeof(thread_t) * listcount);
298 #endif /* !DARWIN_DONT_PARSE_STACK */
300 static mach_port_t GC_mach_handler_thread;
301 static int GC_use_mach_handler_thread = 0;
303 static struct GC_mach_thread GC_mach_threads[THREAD_TABLE_SZ];
304 static int GC_mach_threads_count;
306 void GC_stop_init() {
307 int i;
309 for (i = 0; i < THREAD_TABLE_SZ; i++) {
310 GC_mach_threads[i].thread = 0;
311 GC_mach_threads[i].already_suspended = 0;
313 GC_mach_threads_count = 0;
316 /* returns true if there's a thread in act_list that wasn't in old_list */
317 int GC_suspend_thread_list(thread_act_array_t act_list, int count,
318 thread_act_array_t old_list, int old_count) {
319 mach_port_t my_thread = mach_thread_self();
320 int i, j;
322 int changed = 0;
324 for(i = 0; i < count; i++) {
325 thread_act_t thread = act_list[i];
326 # if DEBUG_THREADS
327 GC_printf1("Attempting to suspend thread %p\n", thread);
328 # endif
329 /* find the current thread in the old list */
330 int found = 0;
331 for(j = 0; j < old_count; j++) {
332 thread_act_t old_thread = old_list[j];
333 if (old_thread == thread) {
334 found = 1;
335 break;
338 if (!found) {
339 /* add it to the GC_mach_threads list */
340 GC_mach_threads[GC_mach_threads_count].thread = thread;
341 /* default is not suspended */
342 GC_mach_threads[GC_mach_threads_count].already_suspended = 0;
343 changed = 1;
346 if (thread != my_thread &&
347 (!GC_use_mach_handler_thread
348 || (GC_use_mach_handler_thread
349 && GC_mach_handler_thread != thread))) {
350 struct thread_basic_info info;
351 mach_msg_type_number_t outCount = THREAD_INFO_MAX;
352 kern_return_t kern_result = thread_info(thread, THREAD_BASIC_INFO,
353 (thread_info_t)&info, &outCount);
354 if(kern_result != KERN_SUCCESS) {
355 /* the thread may have quit since the thread_threads () call
356 * we mark already_suspended so it's not dealt with anymore later
358 if (!found) {
359 GC_mach_threads[GC_mach_threads_count].already_suspended = TRUE;
360 GC_mach_threads_count++;
362 continue;
364 # if DEBUG_THREADS
365 GC_printf2("Thread state for 0x%lx = %d\n", thread, info.run_state);
366 # endif
367 if (!found) {
368 GC_mach_threads[GC_mach_threads_count].already_suspended = info.suspend_count;
370 if (info.suspend_count) continue;
372 # if DEBUG_THREADS
373 GC_printf1("Suspending 0x%lx\n", thread);
374 # endif
375 /* Suspend the thread */
376 kern_result = thread_suspend(thread);
377 if(kern_result != KERN_SUCCESS) {
378 /* the thread may have quit since the thread_threads () call
379 * we mark already_suspended so it's not dealt with anymore later
381 if (!found) {
382 GC_mach_threads[GC_mach_threads_count].already_suspended = TRUE;
383 GC_mach_threads_count++;
385 continue;
388 if (!found) GC_mach_threads_count++;
390 return changed;
394 /* Caller holds allocation lock. */
395 void GC_stop_world()
397 int i, changes;
398 GC_thread p;
399 mach_port_t my_thread = mach_thread_self();
400 kern_return_t kern_result;
401 thread_act_array_t act_list, prev_list;
402 mach_msg_type_number_t listcount, prevcount;
404 # if DEBUG_THREADS
405 GC_printf1("Stopping the world from 0x%lx\n", mach_thread_self());
406 # endif
408 /* clear out the mach threads list table */
409 GC_stop_init();
411 /* Make sure all free list construction has stopped before we start. */
412 /* No new construction can start, since free list construction is */
413 /* required to acquire and release the GC lock before it starts, */
414 /* and we have the lock. */
415 # ifdef PARALLEL_MARK
416 GC_acquire_mark_lock();
417 GC_ASSERT(GC_fl_builder_count == 0);
418 /* We should have previously waited for it to become zero. */
419 # endif /* PARALLEL_MARK */
421 /* Loop stopping threads until you have gone over the whole list
422 twice without a new one appearing. thread_create() won't
423 return (and thus the thread stop) until the new thread
424 exists, so there is no window whereby you could stop a
425 thread, recognise it is stopped, but then have a new thread
426 it created before stopping show up later.
429 changes = 1;
430 prev_list = NULL;
431 prevcount = 0;
432 do {
433 int result;
434 kern_result = task_threads(current_task(), &act_list, &listcount);
435 result = GC_suspend_thread_list(act_list, listcount,
436 prev_list, prevcount);
437 changes = result;
438 prev_list = act_list;
439 prevcount = listcount;
440 vm_deallocate(current_task(), (vm_address_t)act_list, sizeof(thread_t) * listcount);
441 } while (changes);
444 # ifdef MPROTECT_VDB
445 if(GC_incremental) {
446 extern void GC_mprotect_stop();
447 GC_mprotect_stop();
449 # endif
451 # ifdef PARALLEL_MARK
452 GC_release_mark_lock();
453 # endif
454 #if DEBUG_THREADS
455 GC_printf1("World stopped from 0x%lx\n", my_thread);
456 #endif
459 /* Caller holds allocation lock, and has held it continuously since */
460 /* the world stopped. */
461 void GC_start_world()
463 mach_port_t my_thread = mach_thread_self();
464 int i, j;
465 GC_thread p;
466 kern_return_t kern_result;
467 thread_act_array_t act_list;
468 mach_msg_type_number_t listcount;
469 struct thread_basic_info info;
470 mach_msg_type_number_t outCount = THREAD_INFO_MAX;
472 # if DEBUG_THREADS
473 GC_printf0("World starting\n");
474 # endif
476 # ifdef MPROTECT_VDB
477 if(GC_incremental) {
478 extern void GC_mprotect_resume();
479 GC_mprotect_resume();
481 # endif
483 kern_result = task_threads(current_task(), &act_list, &listcount);
484 for(i = 0; i < listcount; i++) {
485 thread_act_t thread = act_list[i];
486 if (thread != my_thread &&
487 (!GC_use_mach_handler_thread ||
488 (GC_use_mach_handler_thread && GC_mach_handler_thread != thread))) {
489 for(j = 0; j < GC_mach_threads_count; j++) {
490 if (thread == GC_mach_threads[j].thread) {
491 if (GC_mach_threads[j].already_suspended) {
492 # if DEBUG_THREADS
493 GC_printf1("Not resuming already suspended thread %p\n", thread);
494 # endif
495 continue;
497 kern_result = thread_info(thread, THREAD_BASIC_INFO,
498 (thread_info_t)&info, &outCount);
499 if(kern_result != KERN_SUCCESS) ABORT("thread_info failed");
500 # if DEBUG_THREADS
501 GC_printf2("Thread state for 0x%lx = %d\n", thread,
502 info.run_state);
503 GC_printf1("Resuming 0x%lx\n", thread);
504 # endif
505 /* Resume the thread */
506 kern_result = thread_resume(thread);
507 if(kern_result != KERN_SUCCESS) ABORT("thread_resume failed");
512 vm_deallocate(current_task(), (vm_address_t)act_list, sizeof(thread_t) * listcount);
513 # if DEBUG_THREADS
514 GC_printf0("World started\n");
515 # endif
518 void GC_darwin_register_mach_handler_thread(mach_port_t thread) {
519 GC_mach_handler_thread = thread;
520 GC_use_mach_handler_thread = 1;
523 #endif