2006-11-19 Allan Hsu <allan@counterpop.net>
[mono.git] / libgc / darwin_stop_world.c
1 #include "private/pthread_support.h"
2
3 # if defined(GC_DARWIN_THREADS)
4
5 /* From "Inside Mac OS X - Mach-O Runtime Architecture" published by Apple
6    Page 49:
7    "The space beneath the stack pointer, where a new stack frame would normally
8    be allocated, is called the red zone. This area as shown in Figure 3-2 may
9    be used for any purpose as long as a new stack frame does not need to be
10    added to the stack."
11    
12    Page 50: "If a leaf procedure's red zone usage would exceed 224 bytes, then
13    it must set up a stack frame just like routines that call other routines."
14 */
15 #ifdef POWERPC
16 # if CPP_WORDSZ == 32
17 #   define PPC_RED_ZONE_SIZE 224
18 # elif CPP_WORDSZ == 64
19 #   define PPC_RED_ZONE_SIZE 320
20 # endif
21 #endif
22
23 typedef struct StackFrame {
24   unsigned long savedSP;
25   unsigned long savedCR;
26   unsigned long savedLR;
27   unsigned long reserved[2];
28   unsigned long savedRTOC;
29 } StackFrame;
30
31 unsigned long FindTopOfStack(unsigned int stack_start) {
32   StackFrame    *frame;
33   
34   if (stack_start == 0) {
35 # ifdef POWERPC
36 #   if CPP_WORDSZ == 32
37       __asm__ volatile("lwz     %0,0(r1)" : "=r" (frame));
38 #   else
39       __asm__ volatile("ldz     %0,0(r1)" : "=r" (frame));
40 #   endif
41 # endif
42   } else {
43     frame = (StackFrame *)stack_start;
44   }
45
46 # ifdef DEBUG_THREADS
47     /* GC_printf1("FindTopOfStack start at sp = %p\n", frame); */
48 # endif
49   do {
50     if (frame->savedSP == 0) break;
51                 /* if there are no more stack frames, stop */
52
53     frame = (StackFrame*)frame->savedSP;
54
55     /* we do these next two checks after going to the next frame
56        because the LR for the first stack frame in the loop
57        is not set up on purpose, so we shouldn't check it. */
58     if ((frame->savedLR & ~3) == 0) break; /* if the next LR is bogus, stop */
59     if ((~(frame->savedLR) & ~3) == 0) break; /* ditto */
60   } while (1); 
61
62 # ifdef DEBUG_THREADS
63     /* GC_printf1("FindTopOfStack finish at sp = %p\n", frame); */
64 # endif
65
66   return (unsigned long)frame;
67 }       
68
69 #ifdef DARWIN_DONT_PARSE_STACK
70 void GC_push_all_stacks() {
71   int i;
72   kern_return_t r;
73   GC_thread p;
74   pthread_t me;
75   ptr_t lo, hi;
76 #if defined(POWERPC)
77   ppc_thread_state_t state;
78   mach_msg_type_number_t thread_state_count = PPC_THREAD_STATE_COUNT;
79 #elif defined(I386)
80   i386_thread_state_t state;
81   mach_msg_type_number_t thread_state_count = i386_THREAD_STATE_COUNT;
82 #else
83 # error FIXME for non-x86 || ppc architectures
84   mach_msg_type_number_t thread_state_count = MACHINE_THREAD_STATE_COUNT;
85 #endif
86   
87   me = pthread_self();
88   if (!GC_thr_initialized) GC_thr_init();
89   
90   for(i=0;i<THREAD_TABLE_SZ;i++) {
91     for(p=GC_threads[i];p!=0;p=p->next) {
92       if(p -> flags & FINISHED) continue;
93       if(pthread_equal(p->id,me)) {
94         lo = GC_approx_sp();
95       } else {
96         /* Get the thread state (registers, etc) */
97         r = thread_get_state(
98                              p->stop_info.mach_thread,
99                              GC_MACH_THREAD_STATE_FLAVOR,
100                              (natural_t*)&state,
101                              &thread_state_count);
102         if(r != KERN_SUCCESS) ABORT("thread_get_state failed");
103         
104 #if defined(I386)
105         lo = state.esp;
106
107         GC_push_one(state.eax); 
108         GC_push_one(state.ebx); 
109         GC_push_one(state.ecx); 
110         GC_push_one(state.edx); 
111         GC_push_one(state.edi); 
112         GC_push_one(state.esi); 
113         GC_push_one(state.ebp); 
114 #elif defined(POWERPC)
115         lo = (void*)(state.r1 - PPC_RED_ZONE_SIZE);
116         
117         GC_push_one(state.r0); 
118         GC_push_one(state.r2); 
119         GC_push_one(state.r3); 
120         GC_push_one(state.r4); 
121         GC_push_one(state.r5); 
122         GC_push_one(state.r6); 
123         GC_push_one(state.r7); 
124         GC_push_one(state.r8); 
125         GC_push_one(state.r9); 
126         GC_push_one(state.r10); 
127         GC_push_one(state.r11); 
128         GC_push_one(state.r12); 
129         GC_push_one(state.r13); 
130         GC_push_one(state.r14); 
131         GC_push_one(state.r15); 
132         GC_push_one(state.r16); 
133         GC_push_one(state.r17); 
134         GC_push_one(state.r18); 
135         GC_push_one(state.r19); 
136         GC_push_one(state.r20); 
137         GC_push_one(state.r21); 
138         GC_push_one(state.r22); 
139         GC_push_one(state.r23); 
140         GC_push_one(state.r24); 
141         GC_push_one(state.r25); 
142         GC_push_one(state.r26); 
143         GC_push_one(state.r27); 
144         GC_push_one(state.r28); 
145         GC_push_one(state.r29); 
146         GC_push_one(state.r30); 
147         GC_push_one(state.r31);
148 #else
149 # error FIXME for non-x86 || ppc architectures
150 #endif
151       } /* p != me */
152       if(p->flags & MAIN_THREAD)
153         hi = GC_stackbottom;
154       else
155         hi = p->stack_end;
156 #if DEBUG_THREADS
157       GC_printf3("Darwin: Stack for thread 0x%lx = [%lx,%lx)\n",
158                  (unsigned long) p -> id,
159                  (unsigned long) lo,
160                  (unsigned long) hi
161                  );
162 #endif
163       GC_push_all_stack(lo,hi);
164     } /* for(p=GC_threads[i]...) */
165   } /* for(i=0;i<THREAD_TABLE_SZ...) */
166 }
167
168 #else /* !DARWIN_DONT_PARSE_STACK; Use FindTopOfStack() */
169
170 void GC_push_all_stacks() {
171     int i;
172         task_t my_task;
173     kern_return_t r;
174     mach_port_t me;
175     ptr_t lo, hi;
176     thread_act_array_t act_list = 0;
177     mach_msg_type_number_t listcount = 0;
178
179     me = mach_thread_self();
180     if (!GC_thr_initialized) GC_thr_init();
181     
182         my_task = current_task();
183     r = task_threads(my_task, &act_list, &listcount);
184     if(r != KERN_SUCCESS) ABORT("task_threads failed");
185     for(i = 0; i < listcount; i++) {
186       thread_act_t thread = act_list[i];
187       if (thread == me) {
188         lo = GC_approx_sp();
189         hi = (ptr_t)FindTopOfStack(0);
190       } else {
191 #     if defined(POWERPC)
192 #      if CPP_WORDSZ == 32
193         ppc_thread_state_t info;
194 #      else
195         ppc_thread_state64_t info;
196 #      endif
197         mach_msg_type_number_t outCount = THREAD_STATE_MAX;
198         r = thread_get_state(thread, GC_MACH_THREAD_STATE_FLAVOR,
199                              (natural_t *)&info, &outCount);
200         if(r != KERN_SUCCESS) continue;
201
202         lo = (void*)(info.r1 - PPC_RED_ZONE_SIZE);
203         hi = (ptr_t)FindTopOfStack(info.r1);
204
205         GC_push_one(info.r0); 
206         GC_push_one(info.r2); 
207         GC_push_one(info.r3); 
208         GC_push_one(info.r4); 
209         GC_push_one(info.r5); 
210         GC_push_one(info.r6); 
211         GC_push_one(info.r7); 
212         GC_push_one(info.r8); 
213         GC_push_one(info.r9); 
214         GC_push_one(info.r10); 
215         GC_push_one(info.r11); 
216         GC_push_one(info.r12); 
217         GC_push_one(info.r13); 
218         GC_push_one(info.r14); 
219         GC_push_one(info.r15); 
220         GC_push_one(info.r16); 
221         GC_push_one(info.r17); 
222         GC_push_one(info.r18); 
223         GC_push_one(info.r19); 
224         GC_push_one(info.r20); 
225         GC_push_one(info.r21); 
226         GC_push_one(info.r22); 
227         GC_push_one(info.r23); 
228         GC_push_one(info.r24); 
229         GC_push_one(info.r25); 
230         GC_push_one(info.r26); 
231         GC_push_one(info.r27); 
232         GC_push_one(info.r28); 
233         GC_push_one(info.r29); 
234         GC_push_one(info.r30); 
235         GC_push_one(info.r31);
236 #      else
237         /* FIXME: Remove after testing: */
238         WARN("This is completely untested and likely will not work\n", 0);
239         i386_thread_state_t info;
240         mach_msg_type_number_t outCount = THREAD_STATE_MAX;
241         r = thread_get_state(thread, GC_MACH_THREAD_STATE_FLAVOR,
242                              (natural_t *)&info, &outCount);
243         if(r != KERN_SUCCESS) continue;
244
245         lo = (void*)info.esp;
246         hi = (ptr_t)FindTopOfStack(info.esp);
247
248         GC_push_one(info.eax); 
249         GC_push_one(info.ebx); 
250         GC_push_one(info.ecx); 
251         GC_push_one(info.edx); 
252         GC_push_one(info.edi); 
253         GC_push_one(info.esi); 
254         /* GC_push_one(info.ebp);  */
255         /* GC_push_one(info.esp);  */
256         GC_push_one(info.ss); 
257         GC_push_one(info.eip); 
258         GC_push_one(info.cs); 
259         GC_push_one(info.ds); 
260         GC_push_one(info.es); 
261         GC_push_one(info.fs); 
262         GC_push_one(info.gs); 
263 #      endif /* !POWERPC */
264       }
265 #     if DEBUG_THREADS
266        GC_printf3("Darwin: Stack for thread 0x%lx = [%lx,%lx)\n",
267                   (unsigned long) thread,
268                   (unsigned long) lo,
269                   (unsigned long) hi
270                  );
271 #     endif
272       GC_push_all_stack(lo, hi);
273           mach_port_deallocate(my_task, thread);
274     } /* for(p=GC_threads[i]...) */
275     vm_deallocate(my_task, (vm_address_t)act_list, sizeof(thread_t) * listcount);
276         mach_port_deallocate(my_task, me);
277 }
278 #endif /* !DARWIN_DONT_PARSE_STACK */
279
280 static mach_port_t GC_mach_handler_thread;
281 static int GC_use_mach_handler_thread = 0;
282
283 static struct GC_mach_thread GC_mach_threads[THREAD_TABLE_SZ];
284 static int GC_mach_threads_count;
285
286 void GC_stop_init() {
287   int i;
288
289   for (i = 0; i < THREAD_TABLE_SZ; i++) {
290     GC_mach_threads[i].thread = 0;
291     GC_mach_threads[i].already_suspended = 0;
292   }
293   GC_mach_threads_count = 0;
294 }
295
296 /* returns true if there's a thread in act_list that wasn't in old_list */
297 int GC_suspend_thread_list(thread_act_array_t act_list, int count, 
298                            thread_act_array_t old_list, int old_count) {
299   mach_port_t my_thread = mach_thread_self();
300   int i, j;
301
302   int changed = 0;
303
304   for(i = 0; i < count; i++) {
305     thread_act_t thread = act_list[i];
306 #   if DEBUG_THREADS 
307       GC_printf1("Attempting to suspend thread %p\n", thread);
308 #   endif
309     /* find the current thread in the old list */
310     int found = 0;
311     for(j = 0; j < old_count; j++) {
312       thread_act_t old_thread = old_list[j];
313       if (old_thread == thread) {
314         found = 1;
315         break;
316       }
317     }
318     if (!found) {
319       /* add it to the GC_mach_threads list */
320       GC_mach_threads[GC_mach_threads_count].thread = thread;
321       /* default is not suspended */
322       GC_mach_threads[GC_mach_threads_count].already_suspended = 0;
323       changed = 1;
324     }      
325
326     if (thread != my_thread &&
327         (!GC_use_mach_handler_thread
328          || (GC_use_mach_handler_thread
329              && GC_mach_handler_thread != thread))) {
330       struct thread_basic_info info;
331       mach_msg_type_number_t outCount = THREAD_INFO_MAX;
332       kern_return_t kern_result = thread_info(thread, THREAD_BASIC_INFO,
333                                 (thread_info_t)&info, &outCount);
334       if(kern_result != KERN_SUCCESS) {
335         /* the thread may have quit since the thread_threads () call 
336          * we mark already_suspended so it's not dealt with anymore later
337          */
338         if (!found) {
339           GC_mach_threads[GC_mach_threads_count].already_suspended = TRUE;
340           GC_mach_threads_count++;
341         }
342         continue;
343       }
344 #     if DEBUG_THREADS
345         GC_printf2("Thread state for 0x%lx = %d\n", thread, info.run_state);
346 #     endif
347       if (!found) {
348         GC_mach_threads[GC_mach_threads_count].already_suspended = info.suspend_count;
349       }
350       if (info.suspend_count) continue;
351       
352 #     if DEBUG_THREADS
353         GC_printf1("Suspending 0x%lx\n", thread);
354 #     endif
355       /* Suspend the thread */
356       kern_result = thread_suspend(thread);
357       if(kern_result != KERN_SUCCESS) {
358         /* the thread may have quit since the thread_threads () call 
359          * we mark already_suspended so it's not dealt with anymore later
360          */
361         if (!found) {
362           GC_mach_threads[GC_mach_threads_count].already_suspended = TRUE;
363           GC_mach_threads_count++;
364         }
365         continue;
366       }
367     } 
368     if (!found) GC_mach_threads_count++;
369   }
370   
371   mach_port_deallocate(current_task(), my_thread);
372   return changed;
373 }
374
375
376 /* Caller holds allocation lock.        */
377 void GC_stop_world()
378 {
379   int i, changes;
380     GC_thread p;
381         task_t my_task = current_task();
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;
386     
387 #   if DEBUG_THREADS
388       GC_printf1("Stopping the world from 0x%lx\n", mach_thread_self());
389 #   endif
390
391     /* clear out the mach threads list table */
392     GC_stop_init(); 
393        
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 */
403
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.
410       */
411       
412       changes = 1;
413       prev_list = NULL;
414       prevcount = 0;
415       do {
416         int result;               
417         kern_result = task_threads(my_task, &act_list, &listcount);
418         
419         if(kern_result == KERN_SUCCESS) {       
420                 result = GC_suspend_thread_list(act_list, listcount,
421                                                                                 prev_list, prevcount);
422                 changes = result;
423                 
424                 if(prev_list != NULL) {
425                         for(i = 0; i < prevcount; i++)
426                                 mach_port_deallocate(my_task, prev_list[i]);
427                         
428                         vm_deallocate(my_task, (vm_address_t)prev_list, sizeof(thread_t) * prevcount);
429                 }
430                 
431                 prev_list = act_list;
432                 prevcount = listcount;
433         }               
434       } while (changes);
435      
436           for(i = 0; i < listcount; i++)
437                   mach_port_deallocate(my_task, act_list[i]);
438           
439           vm_deallocate(my_task, (vm_address_t)act_list, sizeof(thread_t) * listcount);
440           
441  
442 #   ifdef MPROTECT_VDB
443       if(GC_incremental) {
444         extern void GC_mprotect_stop();
445         GC_mprotect_stop();
446       }
447 #   endif
448     
449 #   ifdef PARALLEL_MARK
450       GC_release_mark_lock();
451 #   endif
452     #if DEBUG_THREADS
453       GC_printf1("World stopped from 0x%lx\n", my_thread);
454     #endif
455           
456           mach_port_deallocate(my_task, my_thread);
457 }
458
459 /* Caller holds allocation lock, and has held it continuously since     */
460 /* the world stopped.                                                   */
461 void GC_start_world()
462 {
463   task_t my_task = current_task();
464   mach_port_t my_thread = mach_thread_self();
465   int i, j;
466   GC_thread p;
467   kern_return_t kern_result;
468   thread_act_array_t act_list;
469   mach_msg_type_number_t listcount;
470   struct thread_basic_info info;
471   mach_msg_type_number_t outCount = THREAD_INFO_MAX;
472   
473 #   if DEBUG_THREADS
474       GC_printf0("World starting\n");
475 #   endif
476
477 #   ifdef MPROTECT_VDB
478       if(GC_incremental) {
479         extern void GC_mprotect_resume();
480         GC_mprotect_resume();
481       }
482 #   endif
483
484     kern_result = task_threads(my_task, &act_list, &listcount);
485     for(i = 0; i < listcount; i++) {
486       thread_act_t thread = act_list[i];
487       if (thread != my_thread &&
488           (!GC_use_mach_handler_thread ||
489            (GC_use_mach_handler_thread && GC_mach_handler_thread != thread))) {
490         for(j = 0; j < GC_mach_threads_count; j++) {
491           if (thread == GC_mach_threads[j].thread) {
492             if (GC_mach_threads[j].already_suspended) {
493 #             if DEBUG_THREADS
494                 GC_printf1("Not resuming already suspended thread %p\n", thread);
495 #             endif
496               continue;
497             }
498             kern_result = thread_info(thread, THREAD_BASIC_INFO,
499                                       (thread_info_t)&info, &outCount);
500             if(kern_result != KERN_SUCCESS) continue;
501 #           if DEBUG_THREADS
502               GC_printf2("Thread state for 0x%lx = %d\n", thread,
503                          info.run_state);
504               GC_printf1("Resuming 0x%lx\n", thread);
505 #           endif
506             /* Resume the thread */
507             kern_result = thread_resume(thread);
508             if(kern_result != KERN_SUCCESS) continue;
509           } 
510         }
511       }
512           
513           mach_port_deallocate(my_task, thread);
514     }
515     vm_deallocate(my_task, (vm_address_t)act_list, sizeof(thread_t) * listcount);
516         
517         mach_port_deallocate(my_task, my_thread);
518 #   if DEBUG_THREADS
519      GC_printf0("World started\n");
520 #   endif
521 }
522
523 void GC_darwin_register_mach_handler_thread(mach_port_t thread) {
524   GC_mach_handler_thread = thread;
525   GC_use_mach_handler_thread = 1;
526 }
527
528 #endif