* config/xtensa/xtensa.c (function_arg_boundary): New.
[official-gcc.git] / boehm-gc / darwin_stop_world.c
blob895fdb61a6472cece4dfd056580313bfc6300f5a
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 #if defined(__ppc__)
26 # define THREAD_STATE ppc_thread_state_t
27 # if defined (HAS_PPC_THREAD_STATE_R0)
28 # define THREAD_FLD(x) x
29 # elif defined (HAS_PPC_THREAD_STATE___R0)
30 # define THREAD_FLD(x) __ ## x
31 # else
32 # error can not work out how to access fields of ppc_thread_state_t
33 # endif
34 #elif defined(__ppc64__)
35 # define THREAD_STATE ppc_thread_state64_t
36 # if defined (HAS_PPC_THREAD_STATE64_R0)
37 # define THREAD_FLD(x) x
38 # elif defined (HAS_PPC_THREAD_STATE64___R0)
39 # define THREAD_FLD(x) __ ## x
40 # else
41 # error can not work out how to access fields of ppc_thread_state64_t
42 # endif
43 #elif defined(__i386__)
44 # define THREAD_STATE i386_thread_state_t
45 # if defined (HAS_I386_THREAD_STATE_EAX)
46 # define THREAD_FLD(x) x
47 # elif defined (HAS_I386_THREAD_STATE___EAX)
48 # define THREAD_FLD(x) __ ## x
49 # else
50 # error can not work out how to access fields of i386_thread_state_t
51 # endif
52 #else
53 # error unknown architecture
54 #endif
56 typedef struct StackFrame {
57 unsigned long savedSP;
58 unsigned long savedCR;
59 unsigned long savedLR;
60 unsigned long reserved[2];
61 unsigned long savedRTOC;
62 } StackFrame;
64 unsigned long FindTopOfStack(unsigned int stack_start) {
65 StackFrame *frame;
67 if (stack_start == 0) {
68 # ifdef POWERPC
69 # if CPP_WORDSZ == 32
70 __asm__ volatile("lwz %0,0(r1)" : "=r" (frame));
71 # else
72 __asm__ volatile("ld %0,0(r1)" : "=r" (frame));
73 # endif
74 # endif
75 } else {
76 frame = (StackFrame *)stack_start;
79 # ifdef DEBUG_THREADS
80 /* GC_printf1("FindTopOfStack start at sp = %p\n", frame); */
81 # endif
82 do {
83 if (frame->savedSP == 0) break;
84 /* if there are no more stack frames, stop */
86 frame = (StackFrame*)frame->savedSP;
88 /* we do these next two checks after going to the next frame
89 because the LR for the first stack frame in the loop
90 is not set up on purpose, so we shouldn't check it. */
91 if ((frame->savedLR & ~3) == 0) break; /* if the next LR is bogus, stop */
92 if ((~(frame->savedLR) & ~3) == 0) break; /* ditto */
93 } while (1);
95 # ifdef DEBUG_THREADS
96 /* GC_printf1("FindTopOfStack finish at sp = %p\n", frame); */
97 # endif
99 return (unsigned long)frame;
102 #ifdef DARWIN_DONT_PARSE_STACK
103 void GC_push_all_stacks() {
104 int i;
105 kern_return_t r;
106 GC_thread p;
107 pthread_t me;
108 ptr_t lo, hi;
109 THREAD_STATE state;
110 mach_msg_type_number_t thread_state_count = MACHINE_THREAD_STATE_COUNT;
112 me = pthread_self();
113 if (!GC_thr_initialized) GC_thr_init();
115 for(i=0;i<THREAD_TABLE_SZ;i++) {
116 for(p=GC_threads[i];p!=0;p=p->next) {
117 if(p -> flags & FINISHED) continue;
118 if(pthread_equal(p->id,me)) {
119 lo = GC_approx_sp();
120 } else {
121 /* Get the thread state (registers, etc) */
122 r = thread_get_state(
123 p->stop_info.mach_thread,
124 MACHINE_THREAD_STATE,
125 (natural_t*)&state,
126 &thread_state_count);
127 if(r != KERN_SUCCESS) ABORT("thread_get_state failed");
129 lo = (void*)(state . THREAD_FLD (r1) - PPC_RED_ZONE_SIZE);
131 GC_push_one(state . THREAD_FLD (r0));
132 GC_push_one(state . THREAD_FLD (r2));
133 GC_push_one(state . THREAD_FLD (r3));
134 GC_push_one(state . THREAD_FLD (r4));
135 GC_push_one(state . THREAD_FLD (r5));
136 GC_push_one(state . THREAD_FLD (r6));
137 GC_push_one(state . THREAD_FLD (r7));
138 GC_push_one(state . THREAD_FLD (r8));
139 GC_push_one(state . THREAD_FLD (r9));
140 GC_push_one(state . THREAD_FLD (r10));
141 GC_push_one(state . THREAD_FLD (r11));
142 GC_push_one(state . THREAD_FLD (r12));
143 GC_push_one(state . THREAD_FLD (r13));
144 GC_push_one(state . THREAD_FLD (r14));
145 GC_push_one(state . THREAD_FLD (r15));
146 GC_push_one(state . THREAD_FLD (r16));
147 GC_push_one(state . THREAD_FLD (r17));
148 GC_push_one(state . THREAD_FLD (r18));
149 GC_push_one(state . THREAD_FLD (r19));
150 GC_push_one(state . THREAD_FLD (r20));
151 GC_push_one(state . THREAD_FLD (r21));
152 GC_push_one(state . THREAD_FLD (r22));
153 GC_push_one(state . THREAD_FLD (r23));
154 GC_push_one(state . THREAD_FLD (r24));
155 GC_push_one(state . THREAD_FLD (r25));
156 GC_push_one(state . THREAD_FLD (r26));
157 GC_push_one(state . THREAD_FLD (r27));
158 GC_push_one(state . THREAD_FLD (r28));
159 GC_push_one(state . THREAD_FLD (r29));
160 GC_push_one(state . THREAD_FLD (r30));
161 GC_push_one(state . THREAD_FLD (r31));
162 } /* p != me */
163 if(p->flags & MAIN_THREAD)
164 hi = GC_stackbottom;
165 else
166 hi = p->stack_end;
167 #if DEBUG_THREADS
168 GC_printf3("Darwin: Stack for thread 0x%lx = [%lx,%lx)\n",
169 (unsigned long) p -> id,
170 (unsigned long) lo,
171 (unsigned long) hi
173 #endif
174 GC_push_all_stack(lo,hi);
175 } /* for(p=GC_threads[i]...) */
176 } /* for(i=0;i<THREAD_TABLE_SZ...) */
179 #else /* !DARWIN_DONT_PARSE_STACK; Use FindTopOfStack() */
181 void GC_push_all_stacks() {
182 int i;
183 kern_return_t r;
184 mach_port_t me;
185 ptr_t lo, hi;
186 thread_act_array_t act_list = 0;
187 mach_msg_type_number_t listcount = 0;
189 me = mach_thread_self();
190 if (!GC_thr_initialized) GC_thr_init();
192 r = task_threads(current_task(), &act_list, &listcount);
193 if(r != KERN_SUCCESS) ABORT("task_threads failed");
194 for(i = 0; i < listcount; i++) {
195 thread_act_t thread = act_list[i];
196 if (thread == me) {
197 lo = GC_approx_sp();
198 hi = (ptr_t)FindTopOfStack(0);
199 } else {
200 # if defined(__ppc__) || defined(__ppc64__)
201 THREAD_STATE info;
202 mach_msg_type_number_t outCount = THREAD_STATE_MAX;
203 r = thread_get_state(thread, MACHINE_THREAD_STATE,
204 (natural_t *)&info, &outCount);
205 if(r != KERN_SUCCESS) ABORT("task_get_state failed");
207 lo = (void*)(info . THREAD_FLD (r1) - PPC_RED_ZONE_SIZE);
208 hi = (ptr_t)FindTopOfStack(info . THREAD_FLD (r1));
210 GC_push_one(info . THREAD_FLD (r0));
211 GC_push_one(info . THREAD_FLD (r2));
212 GC_push_one(info . THREAD_FLD (r3));
213 GC_push_one(info . THREAD_FLD (r4));
214 GC_push_one(info . THREAD_FLD (r5));
215 GC_push_one(info . THREAD_FLD (r6));
216 GC_push_one(info . THREAD_FLD (r7));
217 GC_push_one(info . THREAD_FLD (r8));
218 GC_push_one(info . THREAD_FLD (r9));
219 GC_push_one(info . THREAD_FLD (r10));
220 GC_push_one(info . THREAD_FLD (r11));
221 GC_push_one(info . THREAD_FLD (r12));
222 GC_push_one(info . THREAD_FLD (r13));
223 GC_push_one(info . THREAD_FLD (r14));
224 GC_push_one(info . THREAD_FLD (r15));
225 GC_push_one(info . THREAD_FLD (r16));
226 GC_push_one(info . THREAD_FLD (r17));
227 GC_push_one(info . THREAD_FLD (r18));
228 GC_push_one(info . THREAD_FLD (r19));
229 GC_push_one(info . THREAD_FLD (r20));
230 GC_push_one(info . THREAD_FLD (r21));
231 GC_push_one(info . THREAD_FLD (r22));
232 GC_push_one(info . THREAD_FLD (r23));
233 GC_push_one(info . THREAD_FLD (r24));
234 GC_push_one(info . THREAD_FLD (r25));
235 GC_push_one(info . THREAD_FLD (r26));
236 GC_push_one(info . THREAD_FLD (r27));
237 GC_push_one(info . THREAD_FLD (r28));
238 GC_push_one(info . THREAD_FLD (r29));
239 GC_push_one(info . THREAD_FLD (r30));
240 GC_push_one(info . THREAD_FLD (r31));
241 # else
242 /* FIXME: Remove after testing: */
243 WARN("This is completely untested and likely will not work\n", 0);
244 THREAD_STATE info;
245 mach_msg_type_number_t outCount = THREAD_STATE_MAX;
246 r = thread_get_state(thread, MACHINE_THREAD_STATE,
247 (natural_t *)&info, &outCount);
248 if(r != KERN_SUCCESS) ABORT("task_get_state failed");
250 lo = (void*)info . THREAD_FLD (esp);
251 hi = (ptr_t)FindTopOfStack(info . THREAD_FLD (esp));
253 GC_push_one(info . THREAD_FLD (eax));
254 GC_push_one(info . THREAD_FLD (ebx));
255 GC_push_one(info . THREAD_FLD (ecx));
256 GC_push_one(info . THREAD_FLD (edx));
257 GC_push_one(info . THREAD_FLD (edi));
258 GC_push_one(info . THREAD_FLD (esi));
259 /* GC_push_one(info . THREAD_FLD (ebp)); */
260 /* GC_push_one(info . THREAD_FLD (esp)); */
261 GC_push_one(info . THREAD_FLD (ss));
262 GC_push_one(info . THREAD_FLD (eip));
263 GC_push_one(info . THREAD_FLD (cs));
264 GC_push_one(info . THREAD_FLD (ds));
265 GC_push_one(info . THREAD_FLD (es));
266 GC_push_one(info . THREAD_FLD (fs));
267 GC_push_one(info . THREAD_FLD (gs));
268 # endif /* !POWERPC */
270 # if DEBUG_THREADS
271 GC_printf3("Darwin: Stack for thread 0x%lx = [%lx,%lx)\n",
272 (unsigned long) thread,
273 (unsigned long) lo,
274 (unsigned long) hi
276 # endif
277 GC_push_all_stack(lo, hi);
278 } /* for(p=GC_threads[i]...) */
279 vm_deallocate(current_task(), (vm_address_t)act_list, sizeof(thread_t) * listcount);
281 #endif /* !DARWIN_DONT_PARSE_STACK */
283 static mach_port_t GC_mach_handler_thread;
284 static int GC_use_mach_handler_thread = 0;
286 static struct GC_mach_thread GC_mach_threads[THREAD_TABLE_SZ];
287 static int GC_mach_threads_count;
289 void GC_stop_init() {
290 int i;
292 for (i = 0; i < THREAD_TABLE_SZ; i++) {
293 GC_mach_threads[i].thread = 0;
294 GC_mach_threads[i].already_suspended = 0;
296 GC_mach_threads_count = 0;
299 /* returns true if there's a thread in act_list that wasn't in old_list */
300 int GC_suspend_thread_list(thread_act_array_t act_list, int count,
301 thread_act_array_t old_list, int old_count) {
302 mach_port_t my_thread = mach_thread_self();
303 int i, j;
305 int changed = 0;
307 for(i = 0; i < count; i++) {
308 thread_act_t thread = act_list[i];
309 # if DEBUG_THREADS
310 GC_printf1("Attempting to suspend thread %p\n", thread);
311 # endif
312 /* find the current thread in the old list */
313 int found = 0;
314 for(j = 0; j < old_count; j++) {
315 thread_act_t old_thread = old_list[j];
316 if (old_thread == thread) {
317 found = 1;
318 break;
321 if (!found) {
322 /* add it to the GC_mach_threads list */
323 GC_mach_threads[GC_mach_threads_count].thread = thread;
324 /* default is not suspended */
325 GC_mach_threads[GC_mach_threads_count].already_suspended = 0;
326 changed = 1;
329 if (thread != my_thread &&
330 (!GC_use_mach_handler_thread
331 || (GC_use_mach_handler_thread
332 && GC_mach_handler_thread != thread))) {
333 struct thread_basic_info info;
334 mach_msg_type_number_t outCount = THREAD_INFO_MAX;
335 kern_return_t kern_result = thread_info(thread, THREAD_BASIC_INFO,
336 (thread_info_t)&info, &outCount);
337 if(kern_result != KERN_SUCCESS) {
338 /* the thread may have quit since the thread_threads () call
339 * we mark already_suspended so it's not dealt with anymore later
341 if (!found) {
342 GC_mach_threads[GC_mach_threads_count].already_suspended = TRUE;
343 GC_mach_threads_count++;
345 continue;
347 # if DEBUG_THREADS
348 GC_printf2("Thread state for 0x%lx = %d\n", thread, info.run_state);
349 # endif
350 if (!found) {
351 GC_mach_threads[GC_mach_threads_count].already_suspended = info.suspend_count;
353 if (info.suspend_count) continue;
355 # if DEBUG_THREADS
356 GC_printf1("Suspending 0x%lx\n", thread);
357 # endif
358 /* Suspend the thread */
359 kern_result = thread_suspend(thread);
360 if(kern_result != KERN_SUCCESS) {
361 /* the thread may have quit since the thread_threads () call
362 * we mark already_suspended so it's not dealt with anymore later
364 if (!found) {
365 GC_mach_threads[GC_mach_threads_count].already_suspended = TRUE;
366 GC_mach_threads_count++;
368 continue;
371 if (!found) GC_mach_threads_count++;
373 return changed;
377 /* Caller holds allocation lock. */
378 void GC_stop_world()
380 int i, changes;
381 GC_thread p;
382 mach_port_t my_thread = mach_thread_self();
383 kern_return_t kern_result;
384 thread_act_array_t act_list, prev_list;
385 mach_msg_type_number_t listcount, prevcount;
387 # if DEBUG_THREADS
388 GC_printf1("Stopping the world from 0x%lx\n", mach_thread_self());
389 # endif
391 /* clear out the mach threads list table */
392 GC_stop_init();
394 /* Make sure all free list construction has stopped before we start. */
395 /* No new construction can start, since free list construction is */
396 /* required to acquire and release the GC lock before it starts, */
397 /* and we have the lock. */
398 # ifdef PARALLEL_MARK
399 GC_acquire_mark_lock();
400 GC_ASSERT(GC_fl_builder_count == 0);
401 /* We should have previously waited for it to become zero. */
402 # endif /* PARALLEL_MARK */
404 /* Loop stopping threads until you have gone over the whole list
405 twice without a new one appearing. thread_create() won't
406 return (and thus the thread stop) until the new thread
407 exists, so there is no window whereby you could stop a
408 thread, recognise it is stopped, but then have a new thread
409 it created before stopping show up later.
412 changes = 1;
413 prev_list = NULL;
414 prevcount = 0;
415 do {
416 int result;
417 kern_result = task_threads(current_task(), &act_list, &listcount);
418 result = GC_suspend_thread_list(act_list, listcount,
419 prev_list, prevcount);
420 changes = result;
421 prev_list = act_list;
422 prevcount = listcount;
423 vm_deallocate(current_task(), (vm_address_t)act_list, sizeof(thread_t) * listcount);
424 } while (changes);
427 # ifdef MPROTECT_VDB
428 if(GC_incremental) {
429 extern void GC_mprotect_stop();
430 GC_mprotect_stop();
432 # endif
434 # ifdef PARALLEL_MARK
435 GC_release_mark_lock();
436 # endif
437 #if DEBUG_THREADS
438 GC_printf1("World stopped from 0x%lx\n", my_thread);
439 #endif
442 /* Caller holds allocation lock, and has held it continuously since */
443 /* the world stopped. */
444 void GC_start_world()
446 mach_port_t my_thread = mach_thread_self();
447 int i, j;
448 GC_thread p;
449 kern_return_t kern_result;
450 thread_act_array_t act_list;
451 mach_msg_type_number_t listcount;
452 struct thread_basic_info info;
453 mach_msg_type_number_t outCount = THREAD_INFO_MAX;
455 # if DEBUG_THREADS
456 GC_printf0("World starting\n");
457 # endif
459 # ifdef MPROTECT_VDB
460 if(GC_incremental) {
461 extern void GC_mprotect_resume();
462 GC_mprotect_resume();
464 # endif
466 kern_result = task_threads(current_task(), &act_list, &listcount);
467 for(i = 0; i < listcount; i++) {
468 thread_act_t thread = act_list[i];
469 if (thread != my_thread &&
470 (!GC_use_mach_handler_thread ||
471 (GC_use_mach_handler_thread && GC_mach_handler_thread != thread))) {
472 for(j = 0; j < GC_mach_threads_count; j++) {
473 if (thread == GC_mach_threads[j].thread) {
474 if (GC_mach_threads[j].already_suspended) {
475 # if DEBUG_THREADS
476 GC_printf1("Not resuming already suspended thread %p\n", thread);
477 # endif
478 continue;
480 kern_result = thread_info(thread, THREAD_BASIC_INFO,
481 (thread_info_t)&info, &outCount);
482 if(kern_result != KERN_SUCCESS) ABORT("thread_info failed");
483 # if DEBUG_THREADS
484 GC_printf2("Thread state for 0x%lx = %d\n", thread,
485 info.run_state);
486 GC_printf1("Resuming 0x%lx\n", thread);
487 # endif
488 /* Resume the thread */
489 kern_result = thread_resume(thread);
490 if(kern_result != KERN_SUCCESS) ABORT("thread_resume failed");
495 vm_deallocate(current_task(), (vm_address_t)act_list, sizeof(thread_t) * listcount);
496 # if DEBUG_THREADS
497 GC_printf0("World started\n");
498 # endif
501 void GC_darwin_register_mach_handler_thread(mach_port_t thread) {
502 GC_mach_handler_thread = thread;
503 GC_use_mach_handler_thread = 1;
506 #endif