new Stack memory allocation, use of unused arg regs as temp disabled
[cacao.git] / src / boehm-gc / darwin_stop_world.c
1 #include "config.h"
2
3 #include "private/pthread_support.h"
4
5 # if defined(GC_DARWIN_THREADS)
6
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."
13    
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."
16 */
17 #define PPC_RED_ZONE_SIZE 224
18
19 /* Not 64-bit clean. Wait until Apple defines their 64-bit ABI */
20 typedef struct StackFrame {
21   unsigned int  savedSP;
22   unsigned int  savedCR;
23   unsigned int  savedLR;
24   unsigned int  reserved[2];
25   unsigned int  savedRTOC;
26 } StackFrame;
27
28
29 unsigned int FindTopOfStack(unsigned int stack_start) {
30   StackFrame    *frame;
31   
32   if (stack_start == 0) {
33     __asm__ volatile("lwz       %0,0(r1)" : "=r" (frame));
34   } else {
35     frame = (StackFrame *)stack_start;
36   }
37
38 # ifdef DEBUG_THREADS
39     /* GC_printf1("FindTopOfStack start at sp = %p\n", frame); */
40 # endif
41   do {
42     if (frame->savedSP == NULL) break;
43                 /* if there are no more stack frames, stop */
44
45     frame = (StackFrame*)frame->savedSP;
46
47     /* we do these next two checks after going to the next frame
48        because the LR for the first stack frame in the loop
49        is not set up on purpose, so we shouldn't check it. */
50     if ((frame->savedLR & ~3) == 0) break; /* if the next LR is bogus, stop */
51     if ((~(frame->savedLR) & ~3) == 0) break; /* ditto */
52   } while (1); 
53
54 # ifdef DEBUG_THREADS
55     /* GC_printf1("FindTopOfStack finish at sp = %p\n", frame); */
56 # endif
57
58   return (unsigned int)frame;
59 }       
60
61 void GC_push_all_stacks() {
62     int i;
63     kern_return_t r;
64     mach_port_t me;
65     ptr_t lo, hi;
66     thread_act_array_t act_list = 0;
67     mach_msg_type_number_t listcount = 0;
68
69     me = mach_thread_self();
70     if (!GC_thr_initialized) GC_thr_init();
71     
72     r = task_threads(current_task(), &act_list, &listcount);
73     if(r != KERN_SUCCESS) ABORT("task_threads failed");
74     for(i = 0; i < listcount; i++) {
75       thread_act_t thread = act_list[i];
76       if (thread == me) {
77         lo = GC_approx_sp();
78         hi = (ptr_t)FindTopOfStack(0);
79       } else {
80 #      ifdef POWERPC
81         ppc_thread_state_t info;
82         mach_msg_type_number_t outCount = THREAD_STATE_MAX;
83         r = thread_get_state(thread, MACHINE_THREAD_STATE,
84                              (natural_t *)&info, &outCount);
85         if(r != KERN_SUCCESS) ABORT("task_get_state failed");
86
87         lo = (void*)(info.r1 - PPC_RED_ZONE_SIZE);
88         hi = (ptr_t)FindTopOfStack(info.r1);
89
90         GC_push_one(info.r0); 
91         GC_push_one(info.r2); 
92         GC_push_one(info.r3); 
93         GC_push_one(info.r4); 
94         GC_push_one(info.r5); 
95         GC_push_one(info.r6); 
96         GC_push_one(info.r7); 
97         GC_push_one(info.r8); 
98         GC_push_one(info.r9); 
99         GC_push_one(info.r10); 
100         GC_push_one(info.r11); 
101         GC_push_one(info.r12); 
102         GC_push_one(info.r13); 
103         GC_push_one(info.r14); 
104         GC_push_one(info.r15); 
105         GC_push_one(info.r16); 
106         GC_push_one(info.r17); 
107         GC_push_one(info.r18); 
108         GC_push_one(info.r19); 
109         GC_push_one(info.r20); 
110         GC_push_one(info.r21); 
111         GC_push_one(info.r22); 
112         GC_push_one(info.r23); 
113         GC_push_one(info.r24); 
114         GC_push_one(info.r25); 
115         GC_push_one(info.r26); 
116         GC_push_one(info.r27); 
117         GC_push_one(info.r28); 
118         GC_push_one(info.r29); 
119         GC_push_one(info.r30); 
120         GC_push_one(info.r31);
121 #      else
122         /* FIXME: Remove after testing: */
123         WARN("This is completely untested and likely will not work\n", 0);
124         i386_thread_state_t info;
125         mach_msg_type_number_t outCount = THREAD_STATE_MAX;
126         r = thread_get_state(thread, MACHINE_THREAD_STATE,
127                              (natural_t *)&info, &outCount);
128         if(r != KERN_SUCCESS) ABORT("task_get_state failed");
129
130         lo = (void*)info.esp;
131         hi = (ptr_t)FindTopOfStack(info.esp);
132
133         GC_push_one(info.eax); 
134         GC_push_one(info.ebx); 
135         GC_push_one(info.ecx); 
136         GC_push_one(info.edx); 
137         GC_push_one(info.edi); 
138         GC_push_one(info.esi); 
139         /* GC_push_one(info.ebp);  */
140         /* GC_push_one(info.esp);  */
141         GC_push_one(info.ss); 
142         GC_push_one(info.eip); 
143         GC_push_one(info.cs); 
144         GC_push_one(info.ds); 
145         GC_push_one(info.es); 
146         GC_push_one(info.fs); 
147         GC_push_one(info.gs); 
148 #      endif /* !POWERPC */
149       }
150 #     if DEBUG_THREADS
151        GC_printf3("Darwin: Stack for thread 0x%lx = [%lx,%lx)\n",
152                   (unsigned long) thread,
153                   (unsigned long) lo,
154                   (unsigned long) hi
155                  );
156 #     endif
157       GC_push_all_stack(lo, hi); 
158     } /* for(p=GC_threads[i]...) */
159 }
160
161 static mach_port_t GC_mach_handler_thread;
162 static int GC_use_mach_handler_thread = 0;
163
164 static struct GC_mach_thread GC_mach_threads[THREAD_TABLE_SZ];
165 static int GC_mach_threads_count;
166
167 void GC_stop_init() {
168   int i;
169
170   for (i = 0; i < THREAD_TABLE_SZ; i++) {
171     GC_mach_threads[i].thread = 0;
172     GC_mach_threads[i].already_suspended = 0;
173   }
174   GC_mach_threads_count = 0;
175 }
176
177 /* returns true if there's a thread in act_list that wasn't in old_list */
178 int GC_suspend_thread_list(thread_act_array_t act_list, int count, 
179                            thread_act_array_t old_list, int old_count) {
180   mach_port_t my_thread = mach_thread_self();
181   int i, j;
182
183   int changed = 0;
184
185   for(i = 0; i < count; i++) {
186     thread_act_t thread = act_list[i];
187 #   if DEBUG_THREADS 
188       GC_printf1("Attempting to suspend thread %p\n", thread);
189 #   endif
190     /* find the current thread in the old list */
191     int found = 0;
192     for(j = 0; j < old_count; j++) {
193       thread_act_t old_thread = old_list[j];
194       if (old_thread == thread) {
195         found = 1;
196         break;
197       }
198     }
199     if (!found) {
200       /* add it to the GC_mach_threads list */
201       GC_mach_threads[GC_mach_threads_count].thread = thread;
202       /* default is not suspended */
203       GC_mach_threads[GC_mach_threads_count].already_suspended = 0;
204       changed = 1;
205     }      
206
207     if (thread != my_thread &&
208         (!GC_use_mach_handler_thread
209          || (GC_use_mach_handler_thread
210              && GC_mach_handler_thread != thread))) {
211       struct thread_basic_info info;
212       mach_msg_type_number_t outCount = THREAD_INFO_MAX;
213       kern_return_t kern_result = thread_info(thread, THREAD_BASIC_INFO,
214                                 (thread_info_t)&info, &outCount);
215       if(kern_result != KERN_SUCCESS) {
216         /* the thread may have quit since the thread_threads () call 
217          * we mark already_suspended so it's not dealt with anymore later
218          */
219         if (!found) {
220           GC_mach_threads[GC_mach_threads_count].already_suspended = TRUE;
221           GC_mach_threads_count++;
222         }
223         continue;
224       }
225 #     if DEBUG_THREADS
226         GC_printf2("Thread state for 0x%lx = %d\n", thread, info.run_state);
227 #     endif
228       if (!found) {
229         GC_mach_threads[GC_mach_threads_count].already_suspended = info.suspend_count;
230       }
231       if (info.suspend_count) continue;
232       
233 #     if DEBUG_THREADS
234         GC_printf1("Suspending 0x%lx\n", thread);
235 #     endif
236       /* Suspend the thread */
237       kern_result = thread_suspend(thread);
238       if(kern_result != KERN_SUCCESS) {
239         /* the thread may have quit since the thread_threads () call 
240          * we mark already_suspended so it's not dealt with anymore later
241          */
242         if (!found) {
243           GC_mach_threads[GC_mach_threads_count].already_suspended = TRUE;
244           GC_mach_threads_count++;
245         }
246         continue;
247       }
248     } 
249     if (!found) GC_mach_threads_count++;
250   }
251   return changed;
252 }
253
254
255 /* Caller holds allocation lock.        */
256 void GC_stop_world()
257 {
258   int i, changes;
259     GC_thread p;
260     mach_port_t my_thread = mach_thread_self();
261     kern_return_t kern_result;
262     thread_act_array_t act_list, prev_list;
263     mach_msg_type_number_t listcount, prevcount;
264     
265 #   if DEBUG_THREADS
266       GC_printf1("Stopping the world from 0x%lx\n", mach_thread_self());
267 #   endif
268
269     /* clear out the mach threads list table */
270     GC_stop_init(); 
271        
272     /* Make sure all free list construction has stopped before we start. */
273     /* No new construction can start, since free list construction is   */
274     /* required to acquire and release the GC lock before it starts,    */
275     /* and we have the lock.                                            */
276 #   ifdef PARALLEL_MARK
277       GC_acquire_mark_lock();
278       GC_ASSERT(GC_fl_builder_count == 0);
279       /* We should have previously waited for it to become zero. */
280 #   endif /* PARALLEL_MARK */
281
282       /* Loop stopping threads until you have gone over the whole list
283          twice without a new one appearing. thread_create() won't
284          return (and thus the thread stop) until the new thread
285          exists, so there is no window whereby you could stop a
286          thread, recognise it is stopped, but then have a new thread
287          it created before stopping show up later.
288       */
289       
290       changes = 1;
291       prev_list = NULL;
292       prevcount = 0;
293       do {
294         int result;
295         kern_result = task_threads(current_task(), &act_list, &listcount);
296         result = GC_suspend_thread_list(act_list, listcount,
297                                         prev_list, prevcount);
298         changes = result;
299         prev_list = act_list;
300         prevcount = listcount;
301       } while (changes);
302       
303  
304 #   ifdef MPROTECT_VDB
305       if(GC_incremental) {
306         extern void GC_mprotect_stop();
307         GC_mprotect_stop();
308       }
309 #   endif
310     
311 #   ifdef PARALLEL_MARK
312       GC_release_mark_lock();
313 #   endif
314     #if DEBUG_THREADS
315       GC_printf1("World stopped from 0x%lx\n", my_thread);
316     #endif
317 }
318
319 /* Caller holds allocation lock, and has held it continuously since     */
320 /* the world stopped.                                                   */
321 void GC_start_world()
322 {
323   mach_port_t my_thread = mach_thread_self();
324   int i, j;
325   GC_thread p;
326   kern_return_t kern_result;
327   thread_act_array_t act_list;
328   mach_msg_type_number_t listcount;
329   struct thread_basic_info info;
330   mach_msg_type_number_t outCount = THREAD_INFO_MAX;
331   
332 #   if DEBUG_THREADS
333       GC_printf0("World starting\n");
334 #   endif
335
336 #   ifdef MPROTECT_VDB
337       if(GC_incremental) {
338         extern void GC_mprotect_resume();
339         GC_mprotect_resume();
340       }
341 #   endif
342
343     kern_result = task_threads(current_task(), &act_list, &listcount);
344     for(i = 0; i < listcount; i++) {
345       thread_act_t thread = act_list[i];
346       if (thread != my_thread &&
347           (!GC_use_mach_handler_thread ||
348            (GC_use_mach_handler_thread && GC_mach_handler_thread != thread))) {
349         for(j = 0; j < GC_mach_threads_count; j++) {
350           if (thread == GC_mach_threads[j].thread) {
351             if (GC_mach_threads[j].already_suspended) {
352 #             if DEBUG_THREADS
353                 GC_printf1("Not resuming already suspended thread %p\n", thread);
354 #             endif
355               continue;
356             }
357             kern_result = thread_info(thread, THREAD_BASIC_INFO,
358                                       (thread_info_t)&info, &outCount);
359             if(kern_result != KERN_SUCCESS) ABORT("thread_info failed");
360 #           if DEBUG_THREADS
361               GC_printf2("Thread state for 0x%lx = %d\n", thread,
362                          info.run_state);
363               GC_printf1("Resuming 0x%lx\n", thread);
364 #           endif
365             /* Resume the thread */
366             kern_result = thread_resume(thread);
367             if(kern_result != KERN_SUCCESS) ABORT("thread_resume failed");
368           } 
369         }
370       }
371     }
372 #   if DEBUG_THREADS
373      GC_printf0("World started\n");
374 #   endif
375 }
376
377 void GC_darwin_register_mach_handler_thread(mach_port_t thread) {
378   GC_mach_handler_thread = thread;
379   GC_use_mach_handler_thread = 1;
380 }
381
382 #endif