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