In .:
[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 #elif defined(I386)
79   i386_thread_state_t state;
80 #else
81 # error FIXME for non-x86 || ppc architectures
82 #endif
83   mach_msg_type_number_t thread_state_count = MACHINE_THREAD_STATE_COUNT;
84   
85   me = pthread_self();
86   if (!GC_thr_initialized) GC_thr_init();
87   
88   for(i=0;i<THREAD_TABLE_SZ;i++) {
89     for(p=GC_threads[i];p!=0;p=p->next) {
90       if(p -> flags & FINISHED) continue;
91       if(pthread_equal(p->id,me)) {
92         lo = GC_approx_sp();
93       } else {
94         /* Get the thread state (registers, etc) */
95         r = thread_get_state(
96                              p->stop_info.mach_thread,
97                              MACHINE_THREAD_STATE,
98                              (natural_t*)&state,
99                              &thread_state_count);
100         if(r != KERN_SUCCESS) ABORT("thread_get_state failed");
101         
102 #if defined(I386)
103         lo = state.esp;
104
105         GC_push_one(state.eax); 
106         GC_push_one(state.ebx); 
107         GC_push_one(state.ecx); 
108         GC_push_one(state.edx); 
109         GC_push_one(state.edi); 
110         GC_push_one(state.esi); 
111         GC_push_one(state.ebp); 
112 #elif defined(POWERPC)
113         lo = (void*)(state.r1 - PPC_RED_ZONE_SIZE);
114         
115         GC_push_one(state.r0); 
116         GC_push_one(state.r2); 
117         GC_push_one(state.r3); 
118         GC_push_one(state.r4); 
119         GC_push_one(state.r5); 
120         GC_push_one(state.r6); 
121         GC_push_one(state.r7); 
122         GC_push_one(state.r8); 
123         GC_push_one(state.r9); 
124         GC_push_one(state.r10); 
125         GC_push_one(state.r11); 
126         GC_push_one(state.r12); 
127         GC_push_one(state.r13); 
128         GC_push_one(state.r14); 
129         GC_push_one(state.r15); 
130         GC_push_one(state.r16); 
131         GC_push_one(state.r17); 
132         GC_push_one(state.r18); 
133         GC_push_one(state.r19); 
134         GC_push_one(state.r20); 
135         GC_push_one(state.r21); 
136         GC_push_one(state.r22); 
137         GC_push_one(state.r23); 
138         GC_push_one(state.r24); 
139         GC_push_one(state.r25); 
140         GC_push_one(state.r26); 
141         GC_push_one(state.r27); 
142         GC_push_one(state.r28); 
143         GC_push_one(state.r29); 
144         GC_push_one(state.r30); 
145         GC_push_one(state.r31);
146 #else
147 # error FIXME for non-x86 || ppc architectures
148 #endif
149       } /* p != me */
150       if(p->flags & MAIN_THREAD)
151         hi = GC_stackbottom;
152       else
153         hi = p->stack_end;
154 #if DEBUG_THREADS
155       GC_printf3("Darwin: Stack for thread 0x%lx = [%lx,%lx)\n",
156                  (unsigned long) p -> id,
157                  (unsigned long) lo,
158                  (unsigned long) hi
159                  );
160 #endif
161       GC_push_all_stack(lo,hi);
162     } /* for(p=GC_threads[i]...) */
163   } /* for(i=0;i<THREAD_TABLE_SZ...) */
164 }
165
166 #else /* !DARWIN_DONT_PARSE_STACK; Use FindTopOfStack() */
167
168 void GC_push_all_stacks() {
169     int i;
170     kern_return_t r;
171     mach_port_t me;
172     ptr_t lo, hi;
173     thread_act_array_t act_list = 0;
174     mach_msg_type_number_t listcount = 0;
175
176     me = mach_thread_self();
177     if (!GC_thr_initialized) GC_thr_init();
178     
179     r = task_threads(current_task(), &act_list, &listcount);
180     if(r != KERN_SUCCESS) ABORT("task_threads failed");
181     for(i = 0; i < listcount; i++) {
182       thread_act_t thread = act_list[i];
183       if (thread == me) {
184         lo = GC_approx_sp();
185         hi = (ptr_t)FindTopOfStack(0);
186       } else {
187 #     if defined(POWERPC)
188 #      if CPP_WORDSZ == 32
189         ppc_thread_state_t info;
190 #      else
191         ppc_thread_state64_t info;
192 #      endif
193         mach_msg_type_number_t outCount = THREAD_STATE_MAX;
194         r = thread_get_state(thread, MACHINE_THREAD_STATE,
195                              (natural_t *)&info, &outCount);
196         if(r != KERN_SUCCESS) continue;
197
198         lo = (void*)(info.r1 - PPC_RED_ZONE_SIZE);
199         hi = (ptr_t)FindTopOfStack(info.r1);
200
201         GC_push_one(info.r0); 
202         GC_push_one(info.r2); 
203         GC_push_one(info.r3); 
204         GC_push_one(info.r4); 
205         GC_push_one(info.r5); 
206         GC_push_one(info.r6); 
207         GC_push_one(info.r7); 
208         GC_push_one(info.r8); 
209         GC_push_one(info.r9); 
210         GC_push_one(info.r10); 
211         GC_push_one(info.r11); 
212         GC_push_one(info.r12); 
213         GC_push_one(info.r13); 
214         GC_push_one(info.r14); 
215         GC_push_one(info.r15); 
216         GC_push_one(info.r16); 
217         GC_push_one(info.r17); 
218         GC_push_one(info.r18); 
219         GC_push_one(info.r19); 
220         GC_push_one(info.r20); 
221         GC_push_one(info.r21); 
222         GC_push_one(info.r22); 
223         GC_push_one(info.r23); 
224         GC_push_one(info.r24); 
225         GC_push_one(info.r25); 
226         GC_push_one(info.r26); 
227         GC_push_one(info.r27); 
228         GC_push_one(info.r28); 
229         GC_push_one(info.r29); 
230         GC_push_one(info.r30); 
231         GC_push_one(info.r31);
232 #      else
233         /* FIXME: Remove after testing: */
234         WARN("This is completely untested and likely will not work\n", 0);
235         i386_thread_state_t info;
236         mach_msg_type_number_t outCount = THREAD_STATE_MAX;
237         r = thread_get_state(thread, MACHINE_THREAD_STATE,
238                              (natural_t *)&info, &outCount);
239         if(r != KERN_SUCCESS) continue;
240
241         lo = (void*)info.esp;
242         hi = (ptr_t)FindTopOfStack(info.esp);
243
244         GC_push_one(info.eax); 
245         GC_push_one(info.ebx); 
246         GC_push_one(info.ecx); 
247         GC_push_one(info.edx); 
248         GC_push_one(info.edi); 
249         GC_push_one(info.esi); 
250         /* GC_push_one(info.ebp);  */
251         /* GC_push_one(info.esp);  */
252         GC_push_one(info.ss); 
253         GC_push_one(info.eip); 
254         GC_push_one(info.cs); 
255         GC_push_one(info.ds); 
256         GC_push_one(info.es); 
257         GC_push_one(info.fs); 
258         GC_push_one(info.gs); 
259 #      endif /* !POWERPC */
260       }
261 #     if DEBUG_THREADS
262        GC_printf3("Darwin: Stack for thread 0x%lx = [%lx,%lx)\n",
263                   (unsigned long) thread,
264                   (unsigned long) lo,
265                   (unsigned long) hi
266                  );
267 #     endif
268       GC_push_all_stack(lo, hi); 
269     } /* for(p=GC_threads[i]...) */
270     vm_deallocate(current_task(), (vm_address_t)act_list, sizeof(thread_t) * listcount);
271 }
272 #endif /* !DARWIN_DONT_PARSE_STACK */
273
274 static mach_port_t GC_mach_handler_thread;
275 static int GC_use_mach_handler_thread = 0;
276
277 static struct GC_mach_thread GC_mach_threads[THREAD_TABLE_SZ];
278 static int GC_mach_threads_count;
279
280 void GC_stop_init() {
281   int i;
282
283   for (i = 0; i < THREAD_TABLE_SZ; i++) {
284     GC_mach_threads[i].thread = 0;
285     GC_mach_threads[i].already_suspended = 0;
286   }
287   GC_mach_threads_count = 0;
288 }
289
290 /* returns true if there's a thread in act_list that wasn't in old_list */
291 int GC_suspend_thread_list(thread_act_array_t act_list, int count, 
292                            thread_act_array_t old_list, int old_count) {
293   mach_port_t my_thread = mach_thread_self();
294   int i, j;
295
296   int changed = 0;
297
298   for(i = 0; i < count; i++) {
299     thread_act_t thread = act_list[i];
300 #   if DEBUG_THREADS 
301       GC_printf1("Attempting to suspend thread %p\n", thread);
302 #   endif
303     /* find the current thread in the old list */
304     int found = 0;
305     for(j = 0; j < old_count; j++) {
306       thread_act_t old_thread = old_list[j];
307       if (old_thread == thread) {
308         found = 1;
309         break;
310       }
311     }
312     if (!found) {
313       /* add it to the GC_mach_threads list */
314       GC_mach_threads[GC_mach_threads_count].thread = thread;
315       /* default is not suspended */
316       GC_mach_threads[GC_mach_threads_count].already_suspended = 0;
317       changed = 1;
318     }      
319
320     if (thread != my_thread &&
321         (!GC_use_mach_handler_thread
322          || (GC_use_mach_handler_thread
323              && GC_mach_handler_thread != thread))) {
324       struct thread_basic_info info;
325       mach_msg_type_number_t outCount = THREAD_INFO_MAX;
326       kern_return_t kern_result = thread_info(thread, THREAD_BASIC_INFO,
327                                 (thread_info_t)&info, &outCount);
328       if(kern_result != KERN_SUCCESS) {
329         /* the thread may have quit since the thread_threads () call 
330          * we mark already_suspended so it's not dealt with anymore later
331          */
332         if (!found) {
333           GC_mach_threads[GC_mach_threads_count].already_suspended = TRUE;
334           GC_mach_threads_count++;
335         }
336         continue;
337       }
338 #     if DEBUG_THREADS
339         GC_printf2("Thread state for 0x%lx = %d\n", thread, info.run_state);
340 #     endif
341       if (!found) {
342         GC_mach_threads[GC_mach_threads_count].already_suspended = info.suspend_count;
343       }
344       if (info.suspend_count) continue;
345       
346 #     if DEBUG_THREADS
347         GC_printf1("Suspending 0x%lx\n", thread);
348 #     endif
349       /* Suspend the thread */
350       kern_result = thread_suspend(thread);
351       if(kern_result != KERN_SUCCESS) {
352         /* the thread may have quit since the thread_threads () call 
353          * we mark already_suspended so it's not dealt with anymore later
354          */
355         if (!found) {
356           GC_mach_threads[GC_mach_threads_count].already_suspended = TRUE;
357           GC_mach_threads_count++;
358         }
359         continue;
360       }
361     } 
362     if (!found) GC_mach_threads_count++;
363   }
364   return changed;
365 }
366
367
368 /* Caller holds allocation lock.        */
369 void GC_stop_world()
370 {
371   int i, changes;
372     GC_thread p;
373     mach_port_t my_thread = mach_thread_self();
374     kern_return_t kern_result;
375     thread_act_array_t act_list, prev_list;
376     mach_msg_type_number_t listcount, prevcount;
377     
378 #   if DEBUG_THREADS
379       GC_printf1("Stopping the world from 0x%lx\n", mach_thread_self());
380 #   endif
381
382     /* clear out the mach threads list table */
383     GC_stop_init(); 
384        
385     /* Make sure all free list construction has stopped before we start. */
386     /* No new construction can start, since free list construction is   */
387     /* required to acquire and release the GC lock before it starts,    */
388     /* and we have the lock.                                            */
389 #   ifdef PARALLEL_MARK
390       GC_acquire_mark_lock();
391       GC_ASSERT(GC_fl_builder_count == 0);
392       /* We should have previously waited for it to become zero. */
393 #   endif /* PARALLEL_MARK */
394
395       /* Loop stopping threads until you have gone over the whole list
396          twice without a new one appearing. thread_create() won't
397          return (and thus the thread stop) until the new thread
398          exists, so there is no window whereby you could stop a
399          thread, recognise it is stopped, but then have a new thread
400          it created before stopping show up later.
401       */
402       
403       changes = 1;
404       prev_list = NULL;
405       prevcount = 0;
406       do {
407         int result;
408         kern_result = task_threads(current_task(), &act_list, &listcount);
409         result = GC_suspend_thread_list(act_list, listcount,
410                                         prev_list, prevcount);
411         changes = result;
412         prev_list = act_list;
413         prevcount = listcount;
414         vm_deallocate(current_task(), (vm_address_t)act_list, sizeof(thread_t) * listcount);
415       } while (changes);
416       
417  
418 #   ifdef MPROTECT_VDB
419       if(GC_incremental) {
420         extern void GC_mprotect_stop();
421         GC_mprotect_stop();
422       }
423 #   endif
424     
425 #   ifdef PARALLEL_MARK
426       GC_release_mark_lock();
427 #   endif
428     #if DEBUG_THREADS
429       GC_printf1("World stopped from 0x%lx\n", my_thread);
430     #endif
431 }
432
433 /* Caller holds allocation lock, and has held it continuously since     */
434 /* the world stopped.                                                   */
435 void GC_start_world()
436 {
437   mach_port_t my_thread = mach_thread_self();
438   int i, j;
439   GC_thread p;
440   kern_return_t kern_result;
441   thread_act_array_t act_list;
442   mach_msg_type_number_t listcount;
443   struct thread_basic_info info;
444   mach_msg_type_number_t outCount = THREAD_INFO_MAX;
445   
446 #   if DEBUG_THREADS
447       GC_printf0("World starting\n");
448 #   endif
449
450 #   ifdef MPROTECT_VDB
451       if(GC_incremental) {
452         extern void GC_mprotect_resume();
453         GC_mprotect_resume();
454       }
455 #   endif
456
457     kern_result = task_threads(current_task(), &act_list, &listcount);
458     for(i = 0; i < listcount; i++) {
459       thread_act_t thread = act_list[i];
460       if (thread != my_thread &&
461           (!GC_use_mach_handler_thread ||
462            (GC_use_mach_handler_thread && GC_mach_handler_thread != thread))) {
463         for(j = 0; j < GC_mach_threads_count; j++) {
464           if (thread == GC_mach_threads[j].thread) {
465             if (GC_mach_threads[j].already_suspended) {
466 #             if DEBUG_THREADS
467                 GC_printf1("Not resuming already suspended thread %p\n", thread);
468 #             endif
469               continue;
470             }
471             kern_result = thread_info(thread, THREAD_BASIC_INFO,
472                                       (thread_info_t)&info, &outCount);
473             if(kern_result != KERN_SUCCESS) continue;
474 #           if DEBUG_THREADS
475               GC_printf2("Thread state for 0x%lx = %d\n", thread,
476                          info.run_state);
477               GC_printf1("Resuming 0x%lx\n", thread);
478 #           endif
479             /* Resume the thread */
480             kern_result = thread_resume(thread);
481             if(kern_result != KERN_SUCCESS) continue;
482           } 
483         }
484       }
485     }
486     vm_deallocate(current_task(), (vm_address_t)act_list, sizeof(thread_t) * listcount);
487 #   if DEBUG_THREADS
488      GC_printf0("World started\n");
489 #   endif
490 }
491
492 void GC_darwin_register_mach_handler_thread(mach_port_t thread) {
493   GC_mach_handler_thread = thread;
494   GC_use_mach_handler_thread = 1;
495 }
496
497 #endif