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
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
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 #define PPC_RED_ZONE_SIZE 224
19 typedef struct StackFrame
{
20 unsigned long savedSP
;
21 unsigned long savedCR
;
22 unsigned long savedLR
;
23 unsigned long reserved
[2];
24 unsigned long savedRTOC
;
28 unsigned int FindTopOfStack(unsigned int stack_start
) {
31 if (stack_start
== 0) {
32 __asm__
volatile("lwz %0,0(r1)" : "=r" (frame
));
34 frame
= (StackFrame
*)stack_start
;
38 /* GC_printf1("FindTopOfStack start at sp = %p\n", frame); */
41 if (frame
->savedSP
== NULL
) break;
42 /* if there are no more stack frames, stop */
44 frame
= (StackFrame
*)frame
->savedSP
;
46 /* we do these next two checks after going to the next frame
47 because the LR for the first stack frame in the loop
48 is not set up on purpose, so we shouldn't check it. */
49 if ((frame
->savedLR
& ~3) == 0) break; /* if the next LR is bogus, stop */
50 if ((~(frame
->savedLR
) & ~3) == 0) break; /* ditto */
54 /* GC_printf1("FindTopOfStack finish at sp = %p\n", frame); */
57 return (unsigned int)frame
;
60 void GC_push_all_stacks() {
65 thread_act_array_t act_list
= 0;
66 mach_msg_type_number_t listcount
= 0;
68 me
= mach_thread_self();
69 if (!GC_thr_initialized
) GC_thr_init();
71 r
= task_threads(current_task(), &act_list
, &listcount
);
72 if(r
!= KERN_SUCCESS
) ABORT("task_threads failed");
73 for(i
= 0; i
< listcount
; i
++) {
74 thread_act_t thread
= act_list
[i
];
77 hi
= (ptr_t
)FindTopOfStack(0);
80 ppc_thread_state_t info
;
81 mach_msg_type_number_t outCount
= THREAD_STATE_MAX
;
82 r
= thread_get_state(thread
, MACHINE_THREAD_STATE
,
83 (natural_t
*)&info
, &outCount
);
84 if(r
!= KERN_SUCCESS
) ABORT("task_get_state failed");
86 lo
= (void*)(info
.r1
- PPC_RED_ZONE_SIZE
);
87 hi
= (ptr_t
)FindTopOfStack(info
.r1
);
98 GC_push_one(info
.r10
);
99 GC_push_one(info
.r11
);
100 GC_push_one(info
.r12
);
101 GC_push_one(info
.r13
);
102 GC_push_one(info
.r14
);
103 GC_push_one(info
.r15
);
104 GC_push_one(info
.r16
);
105 GC_push_one(info
.r17
);
106 GC_push_one(info
.r18
);
107 GC_push_one(info
.r19
);
108 GC_push_one(info
.r20
);
109 GC_push_one(info
.r21
);
110 GC_push_one(info
.r22
);
111 GC_push_one(info
.r23
);
112 GC_push_one(info
.r24
);
113 GC_push_one(info
.r25
);
114 GC_push_one(info
.r26
);
115 GC_push_one(info
.r27
);
116 GC_push_one(info
.r28
);
117 GC_push_one(info
.r29
);
118 GC_push_one(info
.r30
);
119 GC_push_one(info
.r31
);
121 /* FIXME: Remove after testing: */
122 WARN("This is completely untested and likely will not work\n", 0);
123 i386_thread_state_t info
;
124 mach_msg_type_number_t outCount
= THREAD_STATE_MAX
;
125 r
= thread_get_state(thread
, MACHINE_THREAD_STATE
,
126 (natural_t
*)&info
, &outCount
);
127 if(r
!= KERN_SUCCESS
) ABORT("task_get_state failed");
129 lo
= (void*)info
.esp
;
130 hi
= (ptr_t
)FindTopOfStack(info
.esp
);
132 GC_push_one(info
.eax
);
133 GC_push_one(info
.ebx
);
134 GC_push_one(info
.ecx
);
135 GC_push_one(info
.edx
);
136 GC_push_one(info
.edi
);
137 GC_push_one(info
.esi
);
138 /* GC_push_one(info.ebp); */
139 /* GC_push_one(info.esp); */
140 GC_push_one(info
.ss
);
141 GC_push_one(info
.eip
);
142 GC_push_one(info
.cs
);
143 GC_push_one(info
.ds
);
144 GC_push_one(info
.es
);
145 GC_push_one(info
.fs
);
146 GC_push_one(info
.gs
);
147 # endif /* !POWERPC */
150 GC_printf3("Darwin: Stack for thread 0x%lx = [%lx,%lx)\n",
151 (unsigned long) thread
,
156 GC_push_all_stack(lo
, hi
);
157 } /* for(p=GC_threads[i]...) */
160 static mach_port_t GC_mach_handler_thread
;
161 static int GC_use_mach_handler_thread
= 0;
163 static struct GC_mach_thread GC_mach_threads
[THREAD_TABLE_SZ
];
164 static int GC_mach_threads_count
;
166 void GC_stop_init() {
169 for (i
= 0; i
< THREAD_TABLE_SZ
; i
++) {
170 GC_mach_threads
[i
].thread
= 0;
171 GC_mach_threads
[i
].already_suspended
= 0;
173 GC_mach_threads_count
= 0;
176 /* returns true if there's a thread in act_list that wasn't in old_list */
177 int GC_suspend_thread_list(thread_act_array_t act_list
, int count
,
178 thread_act_array_t old_list
, int old_count
) {
179 mach_port_t my_thread
= mach_thread_self();
184 for(i
= 0; i
< count
; i
++) {
185 thread_act_t thread
= act_list
[i
];
187 GC_printf1("Attempting to suspend thread %p\n", thread
);
189 /* find the current thread in the old list */
191 for(j
= 0; j
< old_count
; j
++) {
192 thread_act_t old_thread
= old_list
[j
];
193 if (old_thread
== thread
) {
199 /* add it to the GC_mach_threads list */
200 GC_mach_threads
[GC_mach_threads_count
].thread
= thread
;
201 /* default is not suspended */
202 GC_mach_threads
[GC_mach_threads_count
].already_suspended
= 0;
206 if (thread
!= my_thread
&&
207 (!GC_use_mach_handler_thread
208 || (GC_use_mach_handler_thread
209 && GC_mach_handler_thread
!= thread
))) {
210 struct thread_basic_info info
;
211 mach_msg_type_number_t outCount
= THREAD_INFO_MAX
;
212 kern_return_t kern_result
= thread_info(thread
, THREAD_BASIC_INFO
,
213 (thread_info_t
)&info
, &outCount
);
214 if(kern_result
!= KERN_SUCCESS
) {
215 /* the thread may have quit since the thread_threads () call
216 * we mark already_suspended so it's not dealt with anymore later
219 GC_mach_threads
[GC_mach_threads_count
].already_suspended
= TRUE
;
220 GC_mach_threads_count
++;
225 GC_printf2("Thread state for 0x%lx = %d\n", thread
, info
.run_state
);
228 GC_mach_threads
[GC_mach_threads_count
].already_suspended
= info
.suspend_count
;
230 if (info
.suspend_count
) continue;
233 GC_printf1("Suspending 0x%lx\n", thread
);
235 /* Suspend the thread */
236 kern_result
= thread_suspend(thread
);
237 if(kern_result
!= KERN_SUCCESS
) {
238 /* the thread may have quit since the thread_threads () call
239 * we mark already_suspended so it's not dealt with anymore later
242 GC_mach_threads
[GC_mach_threads_count
].already_suspended
= TRUE
;
243 GC_mach_threads_count
++;
248 if (!found
) GC_mach_threads_count
++;
254 /* Caller holds allocation lock. */
259 mach_port_t my_thread
= mach_thread_self();
260 kern_return_t kern_result
;
261 thread_act_array_t act_list
, prev_list
;
262 mach_msg_type_number_t listcount
, prevcount
;
265 GC_printf1("Stopping the world from 0x%lx\n", mach_thread_self());
268 /* clear out the mach threads list table */
271 /* Make sure all free list construction has stopped before we start. */
272 /* No new construction can start, since free list construction is */
273 /* required to acquire and release the GC lock before it starts, */
274 /* and we have the lock. */
275 # ifdef PARALLEL_MARK
276 GC_acquire_mark_lock();
277 GC_ASSERT(GC_fl_builder_count
== 0);
278 /* We should have previously waited for it to become zero. */
279 # endif /* PARALLEL_MARK */
281 /* Loop stopping threads until you have gone over the whole list
282 twice without a new one appearing. thread_create() won't
283 return (and thus the thread stop) until the new thread
284 exists, so there is no window whereby you could stop a
285 thread, recognise it is stopped, but then have a new thread
286 it created before stopping show up later.
294 kern_result
= task_threads(current_task(), &act_list
, &listcount
);
295 result
= GC_suspend_thread_list(act_list
, listcount
,
296 prev_list
, prevcount
);
298 prev_list
= act_list
;
299 prevcount
= listcount
;
305 extern void GC_mprotect_stop();
310 # ifdef PARALLEL_MARK
311 GC_release_mark_lock();
314 GC_printf1("World stopped from 0x%lx\n", my_thread
);
318 /* Caller holds allocation lock, and has held it continuously since */
319 /* the world stopped. */
320 void GC_start_world()
322 mach_port_t my_thread
= mach_thread_self();
325 kern_return_t kern_result
;
326 thread_act_array_t act_list
;
327 mach_msg_type_number_t listcount
;
330 GC_printf0("World starting\n");
335 extern void GC_mprotect_resume();
336 GC_mprotect_resume();
340 kern_result
= task_threads(current_task(), &act_list
, &listcount
);
341 for(i
= 0; i
< listcount
; i
++) {
342 thread_act_t thread
= act_list
[i
];
343 if (thread
!= my_thread
&&
344 (!GC_use_mach_handler_thread
||
345 (GC_use_mach_handler_thread
&& GC_mach_handler_thread
!= thread
))) {
346 for(j
= 0; j
< GC_mach_threads_count
; j
++) {
347 if (thread
== GC_mach_threads
[j
].thread
) {
348 if (GC_mach_threads
[j
].already_suspended
) {
350 GC_printf1("Not resuming already suspended thread %p\n", thread
);
354 struct thread_basic_info info
;
355 mach_msg_type_number_t outCount
= THREAD_INFO_MAX
;
356 kern_result
= thread_info(thread
, THREAD_BASIC_INFO
,
357 (thread_info_t
)&info
, &outCount
);
358 if(kern_result
!= KERN_SUCCESS
) ABORT("thread_info failed");
360 GC_printf2("Thread state for 0x%lx = %d\n", thread
,
362 GC_printf1("Resuming 0x%lx\n", thread
);
364 /* Resume the thread */
365 kern_result
= thread_resume(thread
);
366 if(kern_result
!= KERN_SUCCESS
) ABORT("thread_resume failed");
372 GC_printf0("World started\n");
376 void GC_darwin_register_mach_handler_thread(mach_port_t thread
) {
377 GC_mach_handler_thread
= thread
;
378 GC_use_mach_handler_thread
= 1;