| 1 |
/* |
| 2 |
* Copyright (c) 1994 by Xerox Corporation. All rights reserved. |
| 3 |
* Copyright (c) 1996 by Silicon Graphics. All rights reserved. |
| 4 |
* Copyright (c) 1998 by Fergus Henderson. All rights reserved. |
| 5 |
* Copyright (c) 2000-2010 by Hewlett-Packard Development Company. |
| 6 |
* All rights reserved. |
| 7 |
* |
| 8 |
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED |
| 9 |
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK. |
| 10 |
* |
| 11 |
* Permission is hereby granted to use or copy this program |
| 12 |
* for any purpose, provided the above notices are retained on all copies. |
| 13 |
* Permission to modify the code and to distribute modified code is granted, |
| 14 |
* provided the above notices are retained, and a notice that the code was |
| 15 |
* modified is included with the above copyright notice. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include "private/pthread_support.h" |
| 19 |
|
| 20 |
/* This probably needs more porting work to ppc64. */ |
| 21 |
|
| 22 |
#if defined(GC_DARWIN_THREADS) |
| 23 |
|
| 24 |
#include <sys/sysctl.h> |
| 25 |
#include <mach/machine.h> |
| 26 |
|
| 27 |
#if defined(ARM32) && defined(ARM_THREAD_STATE32) |
| 28 |
# include <CoreFoundation/CoreFoundation.h> |
| 29 |
#endif |
| 30 |
|
| 31 |
/* From "Inside Mac OS X - Mach-O Runtime Architecture" published by Apple |
| 32 |
Page 49: |
| 33 |
"The space beneath the stack pointer, where a new stack frame would normally |
| 34 |
be allocated, is called the red zone. This area as shown in Figure 3-2 may |
| 35 |
be used for any purpose as long as a new stack frame does not need to be |
| 36 |
added to the stack." |
| 37 |
|
| 38 |
Page 50: "If a leaf procedure's red zone usage would exceed 224 bytes, then |
| 39 |
it must set up a stack frame just like routines that call other routines." |
| 40 |
*/ |
| 41 |
#ifdef POWERPC |
| 42 |
# if CPP_WORDSZ == 32 |
| 43 |
# define PPC_RED_ZONE_SIZE 224 |
| 44 |
# elif CPP_WORDSZ == 64 |
| 45 |
# define PPC_RED_ZONE_SIZE 320 |
| 46 |
# endif |
| 47 |
#endif |
| 48 |
|
| 49 |
#ifndef DARWIN_DONT_PARSE_STACK |
| 50 |
|
| 51 |
typedef struct StackFrame { |
| 52 |
unsigned long savedSP; |
| 53 |
unsigned long savedCR; |
| 54 |
unsigned long savedLR; |
| 55 |
unsigned long reserved[2]; |
| 56 |
unsigned long savedRTOC; |
| 57 |
} StackFrame; |
| 58 |
|
| 59 |
GC_INNER ptr_t GC_FindTopOfStack(unsigned long stack_start) |
| 60 |
{ |
| 61 |
StackFrame *frame = (StackFrame *)stack_start; |
| 62 |
|
| 63 |
if (stack_start == 0) { |
| 64 |
# ifdef POWERPC |
| 65 |
# if CPP_WORDSZ == 32 |
| 66 |
__asm__ __volatile__ ("lwz %0,0(r1)" : "=r" (frame)); |
| 67 |
# else |
| 68 |
__asm__ __volatile__ ("ld %0,0(r1)" : "=r" (frame)); |
| 69 |
# endif |
| 70 |
# elif defined(ARM32) |
| 71 |
volatile ptr_t sp_reg; |
| 72 |
__asm__ __volatile__ ("mov %0, r7\n" : "=r" (sp_reg)); |
| 73 |
frame = (StackFrame *)sp_reg; |
| 74 |
# elif defined(AARCH64) |
| 75 |
volatile ptr_t sp_reg; |
| 76 |
__asm__ __volatile__ ("mov %0, x29\n" : "=r" (sp_reg)); |
| 77 |
frame = (StackFrame *)sp_reg; |
| 78 |
# else |
| 79 |
# if defined(CPPCHECK) |
| 80 |
GC_noop1((word)&frame); |
| 81 |
# endif |
| 82 |
ABORT("GC_FindTopOfStack(0) is not implemented"); |
| 83 |
# endif |
| 84 |
} |
| 85 |
|
| 86 |
# ifdef DEBUG_THREADS_EXTRA |
| 87 |
GC_log_printf("FindTopOfStack start at sp= %p\n", (void *)frame); |
| 88 |
# endif |
| 89 |
while (frame->savedSP != 0) { /* stop if no more stack frames */ |
| 90 |
unsigned long maskedLR; |
| 91 |
|
| 92 |
frame = (StackFrame*)frame->savedSP; |
| 93 |
|
| 94 |
/* we do these next two checks after going to the next frame |
| 95 |
because the LR for the first stack frame in the loop |
| 96 |
is not set up on purpose, so we shouldn't check it. */ |
| 97 |
maskedLR = frame -> savedLR & ~0x3UL; |
| 98 |
if (0 == maskedLR || ~0x3UL == maskedLR) |
| 99 |
break; /* if the next LR is bogus, stop */ |
| 100 |
} |
| 101 |
# ifdef DEBUG_THREADS_EXTRA |
| 102 |
GC_log_printf("FindTopOfStack finish at sp= %p\n", (void *)frame); |
| 103 |
# endif |
| 104 |
return (ptr_t)frame; |
| 105 |
} |
| 106 |
|
| 107 |
#endif /* !DARWIN_DONT_PARSE_STACK */ |
| 108 |
|
| 109 |
/* GC_query_task_threads controls whether to obtain the list of */ |
| 110 |
/* the threads from the kernel or to use GC_threads table. */ |
| 111 |
#ifdef GC_NO_THREADS_DISCOVERY |
| 112 |
# define GC_query_task_threads FALSE |
| 113 |
#elif defined(GC_DISCOVER_TASK_THREADS) |
| 114 |
# define GC_query_task_threads TRUE |
| 115 |
#else |
| 116 |
STATIC GC_bool GC_query_task_threads = FALSE; |
| 117 |
#endif /* !GC_NO_THREADS_DISCOVERY */ |
| 118 |
|
| 119 |
/* Use implicit threads registration (all task threads excluding the GC */ |
| 120 |
/* special ones are stopped and scanned). Should be called before */ |
| 121 |
/* GC_INIT() (or, at least, before going multi-threaded). Deprecated. */ |
| 122 |
GC_API void GC_CALL GC_use_threads_discovery(void) |
| 123 |
{ |
| 124 |
# if defined(GC_NO_THREADS_DISCOVERY) || defined(DARWIN_DONT_PARSE_STACK) |
| 125 |
ABORT("Darwin task-threads-based stop and push unsupported"); |
| 126 |
# else |
| 127 |
# ifndef GC_ALWAYS_MULTITHREADED |
| 128 |
GC_ASSERT(!GC_need_to_lock); |
| 129 |
# endif |
| 130 |
# ifndef GC_DISCOVER_TASK_THREADS |
| 131 |
GC_query_task_threads = TRUE; |
| 132 |
# endif |
| 133 |
GC_init_parallel(); /* just to be consistent with Win32 one */ |
| 134 |
# endif |
| 135 |
} |
| 136 |
|
| 137 |
#ifndef kCFCoreFoundationVersionNumber_iOS_8_0 |
| 138 |
# define kCFCoreFoundationVersionNumber_iOS_8_0 1140.1 |
| 139 |
#endif |
| 140 |
|
| 141 |
/* Evaluates the stack range for a given thread. Returns the lower */ |
| 142 |
/* bound and sets *phi to the upper one. */ |
| 143 |
STATIC ptr_t GC_stack_range_for(ptr_t *phi, thread_act_t thread, GC_thread p, |
| 144 |
GC_bool thread_blocked, mach_port_t my_thread, |
| 145 |
ptr_t *paltstack_lo, |
| 146 |
ptr_t *paltstack_hi GC_ATTR_UNUSED) |
| 147 |
{ |
| 148 |
ptr_t lo; |
| 149 |
if (thread == my_thread) { |
| 150 |
GC_ASSERT(!thread_blocked); |
| 151 |
lo = GC_approx_sp(); |
| 152 |
# ifndef DARWIN_DONT_PARSE_STACK |
| 153 |
*phi = GC_FindTopOfStack(0); |
| 154 |
# endif |
| 155 |
|
| 156 |
} else if (thread_blocked) { |
| 157 |
# if defined(CPPCHECK) |
| 158 |
if (NULL == p) ABORT("Invalid GC_thread passed to GC_stack_range_for"); |
| 159 |
# endif |
| 160 |
lo = p->stop_info.stack_ptr; |
| 161 |
# ifndef DARWIN_DONT_PARSE_STACK |
| 162 |
*phi = p->topOfStack; |
| 163 |
# endif |
| 164 |
|
| 165 |
} else { |
| 166 |
/* MACHINE_THREAD_STATE_COUNT does not seem to be defined */ |
| 167 |
/* everywhere. Hence we use our own version. Alternatively, */ |
| 168 |
/* we could use THREAD_STATE_MAX (but seems to be not optimal). */ |
| 169 |
kern_return_t kern_result; |
| 170 |
GC_THREAD_STATE_T state; |
| 171 |
|
| 172 |
# if defined(ARM32) && defined(ARM_THREAD_STATE32) |
| 173 |
/* Use ARM_UNIFIED_THREAD_STATE on iOS8+ 32-bit targets and on */ |
| 174 |
/* 64-bit H/W (iOS7+ 32-bit mode). */ |
| 175 |
size_t size; |
| 176 |
static cpu_type_t cputype = 0; |
| 177 |
|
| 178 |
if (cputype == 0) { |
| 179 |
sysctlbyname("hw.cputype", &cputype, &size, NULL, 0); |
| 180 |
} |
| 181 |
if (cputype == CPU_TYPE_ARM64 |
| 182 |
|| kCFCoreFoundationVersionNumber |
| 183 |
>= kCFCoreFoundationVersionNumber_iOS_8_0) { |
| 184 |
arm_unified_thread_state_t unified_state; |
| 185 |
mach_msg_type_number_t unified_thread_state_count |
| 186 |
= ARM_UNIFIED_THREAD_STATE_COUNT; |
| 187 |
# if defined(CPPCHECK) |
| 188 |
# define GC_ARM_UNIFIED_THREAD_STATE 1 |
| 189 |
# else |
| 190 |
# define GC_ARM_UNIFIED_THREAD_STATE ARM_UNIFIED_THREAD_STATE |
| 191 |
# endif |
| 192 |
kern_result = thread_get_state(thread, GC_ARM_UNIFIED_THREAD_STATE, |
| 193 |
(natural_t *)&unified_state, |
| 194 |
&unified_thread_state_count); |
| 195 |
# if !defined(CPPCHECK) |
| 196 |
if (unified_state.ash.flavor != ARM_THREAD_STATE32) { |
| 197 |
ABORT("unified_state flavor should be ARM_THREAD_STATE32"); |
| 198 |
} |
| 199 |
# endif |
| 200 |
state = unified_state; |
| 201 |
} else |
| 202 |
# endif |
| 203 |
/* else */ { |
| 204 |
mach_msg_type_number_t thread_state_count = GC_MACH_THREAD_STATE_COUNT; |
| 205 |
|
| 206 |
/* Get the thread state (registers, etc.) */ |
| 207 |
do { |
| 208 |
kern_result = thread_get_state(thread, GC_MACH_THREAD_STATE, |
| 209 |
(natural_t *)&state, |
| 210 |
&thread_state_count); |
| 211 |
} while (kern_result == KERN_ABORTED); |
| 212 |
} |
| 213 |
# ifdef DEBUG_THREADS |
| 214 |
GC_log_printf("thread_get_state returns %d\n", kern_result); |
| 215 |
# endif |
| 216 |
if (kern_result != KERN_SUCCESS) |
| 217 |
ABORT("thread_get_state failed"); |
| 218 |
|
| 219 |
# if defined(I386) |
| 220 |
lo = (ptr_t)state.THREAD_FLD(esp); |
| 221 |
# ifndef DARWIN_DONT_PARSE_STACK |
| 222 |
*phi = GC_FindTopOfStack(state.THREAD_FLD(esp)); |
| 223 |
# endif |
| 224 |
GC_push_one(state.THREAD_FLD(eax)); |
| 225 |
GC_push_one(state.THREAD_FLD(ebx)); |
| 226 |
GC_push_one(state.THREAD_FLD(ecx)); |
| 227 |
GC_push_one(state.THREAD_FLD(edx)); |
| 228 |
GC_push_one(state.THREAD_FLD(edi)); |
| 229 |
GC_push_one(state.THREAD_FLD(esi)); |
| 230 |
GC_push_one(state.THREAD_FLD(ebp)); |
| 231 |
|
| 232 |
# elif defined(X86_64) |
| 233 |
lo = (ptr_t)state.THREAD_FLD(rsp); |
| 234 |
# ifndef DARWIN_DONT_PARSE_STACK |
| 235 |
*phi = GC_FindTopOfStack(state.THREAD_FLD(rsp)); |
| 236 |
# endif |
| 237 |
GC_push_one(state.THREAD_FLD(rax)); |
| 238 |
GC_push_one(state.THREAD_FLD(rbx)); |
| 239 |
GC_push_one(state.THREAD_FLD(rcx)); |
| 240 |
GC_push_one(state.THREAD_FLD(rdx)); |
| 241 |
GC_push_one(state.THREAD_FLD(rdi)); |
| 242 |
GC_push_one(state.THREAD_FLD(rsi)); |
| 243 |
GC_push_one(state.THREAD_FLD(rbp)); |
| 244 |
/* rsp is skipped. */ |
| 245 |
GC_push_one(state.THREAD_FLD(r8)); |
| 246 |
GC_push_one(state.THREAD_FLD(r9)); |
| 247 |
GC_push_one(state.THREAD_FLD(r10)); |
| 248 |
GC_push_one(state.THREAD_FLD(r11)); |
| 249 |
GC_push_one(state.THREAD_FLD(r12)); |
| 250 |
GC_push_one(state.THREAD_FLD(r13)); |
| 251 |
GC_push_one(state.THREAD_FLD(r14)); |
| 252 |
GC_push_one(state.THREAD_FLD(r15)); |
| 253 |
|
| 254 |
# elif defined(POWERPC) |
| 255 |
lo = (ptr_t)(state.THREAD_FLD(r1) - PPC_RED_ZONE_SIZE); |
| 256 |
# ifndef DARWIN_DONT_PARSE_STACK |
| 257 |
*phi = GC_FindTopOfStack(state.THREAD_FLD(r1)); |
| 258 |
# endif |
| 259 |
GC_push_one(state.THREAD_FLD(r0)); |
| 260 |
/* r1 is skipped. */ |
| 261 |
GC_push_one(state.THREAD_FLD(r2)); |
| 262 |
GC_push_one(state.THREAD_FLD(r3)); |
| 263 |
GC_push_one(state.THREAD_FLD(r4)); |
| 264 |
GC_push_one(state.THREAD_FLD(r5)); |
| 265 |
GC_push_one(state.THREAD_FLD(r6)); |
| 266 |
GC_push_one(state.THREAD_FLD(r7)); |
| 267 |
GC_push_one(state.THREAD_FLD(r8)); |
| 268 |
GC_push_one(state.THREAD_FLD(r9)); |
| 269 |
GC_push_one(state.THREAD_FLD(r10)); |
| 270 |
GC_push_one(state.THREAD_FLD(r11)); |
| 271 |
GC_push_one(state.THREAD_FLD(r12)); |
| 272 |
GC_push_one(state.THREAD_FLD(r13)); |
| 273 |
GC_push_one(state.THREAD_FLD(r14)); |
| 274 |
GC_push_one(state.THREAD_FLD(r15)); |
| 275 |
GC_push_one(state.THREAD_FLD(r16)); |
| 276 |
GC_push_one(state.THREAD_FLD(r17)); |
| 277 |
GC_push_one(state.THREAD_FLD(r18)); |
| 278 |
GC_push_one(state.THREAD_FLD(r19)); |
| 279 |
GC_push_one(state.THREAD_FLD(r20)); |
| 280 |
GC_push_one(state.THREAD_FLD(r21)); |
| 281 |
GC_push_one(state.THREAD_FLD(r22)); |
| 282 |
GC_push_one(state.THREAD_FLD(r23)); |
| 283 |
GC_push_one(state.THREAD_FLD(r24)); |
| 284 |
GC_push_one(state.THREAD_FLD(r25)); |
| 285 |
GC_push_one(state.THREAD_FLD(r26)); |
| 286 |
GC_push_one(state.THREAD_FLD(r27)); |
| 287 |
GC_push_one(state.THREAD_FLD(r28)); |
| 288 |
GC_push_one(state.THREAD_FLD(r29)); |
| 289 |
GC_push_one(state.THREAD_FLD(r30)); |
| 290 |
GC_push_one(state.THREAD_FLD(r31)); |
| 291 |
|
| 292 |
# elif defined(ARM32) |
| 293 |
lo = (ptr_t)state.THREAD_FLD(sp); |
| 294 |
# ifndef DARWIN_DONT_PARSE_STACK |
| 295 |
*phi = GC_FindTopOfStack(state.THREAD_FLD(r[7])); /* fp */ |
| 296 |
# endif |
| 297 |
{ |
| 298 |
int j; |
| 299 |
for (j = 0; j < 7; j++) |
| 300 |
GC_push_one(state.THREAD_FLD(r[j])); |
| 301 |
j++; /* "r7" is skipped (iOS uses it as a frame pointer) */ |
| 302 |
for (; j <= 12; j++) |
| 303 |
GC_push_one(state.THREAD_FLD(r[j])); |
| 304 |
} |
| 305 |
/* "cpsr", "pc" and "sp" are skipped */ |
| 306 |
GC_push_one(state.THREAD_FLD(lr)); |
| 307 |
|
| 308 |
# elif defined(AARCH64) |
| 309 |
lo = (ptr_t)state.THREAD_FLD(sp); |
| 310 |
# ifndef DARWIN_DONT_PARSE_STACK |
| 311 |
*phi = GC_FindTopOfStack(state.THREAD_FLD(fp)); |
| 312 |
# endif |
| 313 |
{ |
| 314 |
int j; |
| 315 |
for (j = 0; j <= 28; j++) { |
| 316 |
GC_push_one(state.THREAD_FLD(x[j])); |
| 317 |
} |
| 318 |
} |
| 319 |
/* "cpsr", "fp", "pc" and "sp" are skipped */ |
| 320 |
GC_push_one(state.THREAD_FLD(lr)); |
| 321 |
|
| 322 |
# elif defined(CPPCHECK) |
| 323 |
lo = NULL; |
| 324 |
# else |
| 325 |
# error FIXME for non-arm/ppc/x86 architectures |
| 326 |
# endif |
| 327 |
} /* thread != my_thread */ |
| 328 |
|
| 329 |
# ifdef DARWIN_DONT_PARSE_STACK |
| 330 |
/* p is guaranteed to be non-NULL regardless of GC_query_task_threads. */ |
| 331 |
*phi = (p->flags & MAIN_THREAD) != 0 ? GC_stackbottom : p->stack_end; |
| 332 |
# endif |
| 333 |
|
| 334 |
/* TODO: Determine p and handle altstack if !DARWIN_DONT_PARSE_STACK */ |
| 335 |
# ifdef DARWIN_DONT_PARSE_STACK |
| 336 |
if (p->altstack != NULL && (word)p->altstack <= (word)lo |
| 337 |
&& (word)lo <= (word)p->altstack + p->altstack_size) { |
| 338 |
*paltstack_lo = lo; |
| 339 |
*paltstack_hi = p->altstack + p->altstack_size; |
| 340 |
lo = p->stack; |
| 341 |
*phi = p->stack + p->stack_size; |
| 342 |
} else |
| 343 |
# endif |
| 344 |
/* else */ { |
| 345 |
*paltstack_lo = NULL; |
| 346 |
} |
| 347 |
# if defined(STACKPTR_CORRECTOR_AVAILABLE) && defined(DARWIN_DONT_PARSE_STACK) |
| 348 |
if (GC_sp_corrector != 0) |
| 349 |
GC_sp_corrector((void **)&lo, (void *)(p -> id)); |
| 350 |
# endif |
| 351 |
# ifdef DEBUG_THREADS |
| 352 |
GC_log_printf("Darwin: Stack for thread %p is [%p,%p)\n", |
| 353 |
(void *)(word)thread, (void *)lo, (void *)(*phi)); |
| 354 |
# endif |
| 355 |
return lo; |
| 356 |
} |
| 357 |
|
| 358 |
GC_INNER void GC_push_all_stacks(void) |
| 359 |
{ |
| 360 |
ptr_t hi, altstack_lo, altstack_hi; |
| 361 |
task_t my_task = current_task(); |
| 362 |
mach_port_t my_thread = mach_thread_self(); |
| 363 |
GC_bool found_me = FALSE; |
| 364 |
int nthreads = 0; |
| 365 |
word total_size = 0; |
| 366 |
mach_msg_type_number_t listcount = (mach_msg_type_number_t)THREAD_TABLE_SZ; |
| 367 |
if (!EXPECT(GC_thr_initialized, TRUE)) |
| 368 |
GC_thr_init(); |
| 369 |
|
| 370 |
# ifndef DARWIN_DONT_PARSE_STACK |
| 371 |
if (GC_query_task_threads) { |
| 372 |
int i; |
| 373 |
kern_return_t kern_result; |
| 374 |
thread_act_array_t act_list = 0; |
| 375 |
|
| 376 |
/* Obtain the list of the threads from the kernel. */ |
| 377 |
kern_result = task_threads(my_task, &act_list, &listcount); |
| 378 |
if (kern_result != KERN_SUCCESS) |
| 379 |
ABORT("task_threads failed"); |
| 380 |
|
| 381 |
for (i = 0; i < (int)listcount; i++) { |
| 382 |
thread_act_t thread = act_list[i]; |
| 383 |
ptr_t lo = GC_stack_range_for(&hi, thread, NULL, FALSE, my_thread, |
| 384 |
&altstack_lo, &altstack_hi); |
| 385 |
|
| 386 |
if (lo) { |
| 387 |
GC_ASSERT((word)lo <= (word)hi); |
| 388 |
total_size += hi - lo; |
| 389 |
GC_push_all_stack(lo, hi); |
| 390 |
} |
| 391 |
/* TODO: Handle altstack */ |
| 392 |
nthreads++; |
| 393 |
if (thread == my_thread) |
| 394 |
found_me = TRUE; |
| 395 |
mach_port_deallocate(my_task, thread); |
| 396 |
} /* for (i=0; ...) */ |
| 397 |
|
| 398 |
vm_deallocate(my_task, (vm_address_t)act_list, |
| 399 |
sizeof(thread_t) * listcount); |
| 400 |
} else |
| 401 |
# endif /* !DARWIN_DONT_PARSE_STACK */ |
| 402 |
/* else */ { |
| 403 |
int i; |
| 404 |
|
| 405 |
for (i = 0; i < (int)listcount; i++) { |
| 406 |
GC_thread p; |
| 407 |
|
| 408 |
for (p = GC_threads[i]; p != NULL; p = p->next) |
| 409 |
if ((p->flags & FINISHED) == 0) { |
| 410 |
thread_act_t thread = (thread_act_t)p->stop_info.mach_thread; |
| 411 |
ptr_t lo = GC_stack_range_for(&hi, thread, p, |
| 412 |
(GC_bool)p->thread_blocked, |
| 413 |
my_thread, &altstack_lo, |
| 414 |
&altstack_hi); |
| 415 |
|
| 416 |
if (lo) { |
| 417 |
GC_ASSERT((word)lo <= (word)hi); |
| 418 |
total_size += hi - lo; |
| 419 |
GC_push_all_stack_sections(lo, hi, p->traced_stack_sect); |
| 420 |
} |
| 421 |
if (altstack_lo) { |
| 422 |
total_size += altstack_hi - altstack_lo; |
| 423 |
GC_push_all_stack(altstack_lo, altstack_hi); |
| 424 |
} |
| 425 |
nthreads++; |
| 426 |
if (thread == my_thread) |
| 427 |
found_me = TRUE; |
| 428 |
} |
| 429 |
} /* for (i=0; ...) */ |
| 430 |
} |
| 431 |
|
| 432 |
mach_port_deallocate(my_task, my_thread); |
| 433 |
GC_VERBOSE_LOG_PRINTF("Pushed %d thread stacks\n", nthreads); |
| 434 |
if (!found_me && !GC_in_thread_creation) |
| 435 |
ABORT("Collecting from unknown thread"); |
| 436 |
GC_total_stacksize = total_size; |
| 437 |
} |
| 438 |
|
| 439 |
#ifndef GC_NO_THREADS_DISCOVERY |
| 440 |
|
| 441 |
# ifdef MPROTECT_VDB |
| 442 |
STATIC mach_port_t GC_mach_handler_thread = 0; |
| 443 |
STATIC GC_bool GC_use_mach_handler_thread = FALSE; |
| 444 |
|
| 445 |
GC_INNER void GC_darwin_register_mach_handler_thread(mach_port_t thread) |
| 446 |
{ |
| 447 |
GC_mach_handler_thread = thread; |
| 448 |
GC_use_mach_handler_thread = TRUE; |
| 449 |
} |
| 450 |
# endif /* MPROTECT_VDB */ |
| 451 |
|
| 452 |
# ifndef GC_MAX_MACH_THREADS |
| 453 |
# define GC_MAX_MACH_THREADS THREAD_TABLE_SZ |
| 454 |
# endif |
| 455 |
|
| 456 |
struct GC_mach_thread { |
| 457 |
thread_act_t thread; |
| 458 |
GC_bool suspended; |
| 459 |
}; |
| 460 |
|
| 461 |
struct GC_mach_thread GC_mach_threads[GC_MAX_MACH_THREADS]; |
| 462 |
STATIC int GC_mach_threads_count = 0; |
| 463 |
/* FIXME: it is better to implement GC_mach_threads as a hash set. */ |
| 464 |
|
| 465 |
/* returns true if there's a thread in act_list that wasn't in old_list */ |
| 466 |
STATIC GC_bool GC_suspend_thread_list(thread_act_array_t act_list, int count, |
| 467 |
thread_act_array_t old_list, |
| 468 |
int old_count, task_t my_task, |
| 469 |
mach_port_t my_thread) |
| 470 |
{ |
| 471 |
int i; |
| 472 |
int j = -1; |
| 473 |
GC_bool changed = FALSE; |
| 474 |
|
| 475 |
for (i = 0; i < count; i++) { |
| 476 |
thread_act_t thread = act_list[i]; |
| 477 |
GC_bool found; |
| 478 |
kern_return_t kern_result; |
| 479 |
|
| 480 |
if (thread == my_thread |
| 481 |
# ifdef MPROTECT_VDB |
| 482 |
|| (GC_mach_handler_thread == thread && GC_use_mach_handler_thread) |
| 483 |
# endif |
| 484 |
# ifdef PARALLEL_MARK |
| 485 |
|| GC_is_mach_marker(thread) /* ignore the parallel markers */ |
| 486 |
# endif |
| 487 |
) { |
| 488 |
/* Do not add our one, parallel marker and the handler threads; */ |
| 489 |
/* consider it as found (e.g., it was processed earlier). */ |
| 490 |
mach_port_deallocate(my_task, thread); |
| 491 |
continue; |
| 492 |
} |
| 493 |
|
| 494 |
/* find the current thread in the old list */ |
| 495 |
found = FALSE; |
| 496 |
{ |
| 497 |
int last_found = j; /* remember the previous found thread index */ |
| 498 |
|
| 499 |
/* Search for the thread starting from the last found one first. */ |
| 500 |
while (++j < old_count) |
| 501 |
if (old_list[j] == thread) { |
| 502 |
found = TRUE; |
| 503 |
break; |
| 504 |
} |
| 505 |
if (!found) { |
| 506 |
/* If not found, search in the rest (beginning) of the list. */ |
| 507 |
for (j = 0; j < last_found; j++) |
| 508 |
if (old_list[j] == thread) { |
| 509 |
found = TRUE; |
| 510 |
break; |
| 511 |
} |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
if (found) { |
| 516 |
/* It is already in the list, skip processing, release mach port. */ |
| 517 |
mach_port_deallocate(my_task, thread); |
| 518 |
continue; |
| 519 |
} |
| 520 |
|
| 521 |
/* add it to the GC_mach_threads list */ |
| 522 |
if (GC_mach_threads_count == GC_MAX_MACH_THREADS) |
| 523 |
ABORT("Too many threads"); |
| 524 |
GC_mach_threads[GC_mach_threads_count].thread = thread; |
| 525 |
/* default is not suspended */ |
| 526 |
GC_mach_threads[GC_mach_threads_count].suspended = FALSE; |
| 527 |
changed = TRUE; |
| 528 |
|
| 529 |
# ifdef DEBUG_THREADS |
| 530 |
GC_log_printf("Suspending %p\n", (void *)(word)thread); |
| 531 |
# endif |
| 532 |
/* Unconditionally suspend the thread. It will do no */ |
| 533 |
/* harm if it is already suspended by the client logic. */ |
| 534 |
GC_acquire_dirty_lock(); |
| 535 |
do { |
| 536 |
kern_result = thread_suspend(thread); |
| 537 |
} while (kern_result == KERN_ABORTED); |
| 538 |
GC_release_dirty_lock(); |
| 539 |
if (kern_result != KERN_SUCCESS) { |
| 540 |
/* The thread may have quit since the thread_threads() call we */ |
| 541 |
/* mark already suspended so it's not dealt with anymore later. */ |
| 542 |
GC_mach_threads[GC_mach_threads_count].suspended = FALSE; |
| 543 |
} else { |
| 544 |
/* Mark the thread as suspended and require resume. */ |
| 545 |
GC_mach_threads[GC_mach_threads_count].suspended = TRUE; |
| 546 |
if (GC_on_thread_event) |
| 547 |
GC_on_thread_event(GC_EVENT_THREAD_SUSPENDED, (void *)(word)thread); |
| 548 |
} |
| 549 |
GC_mach_threads_count++; |
| 550 |
} |
| 551 |
return changed; |
| 552 |
} |
| 553 |
|
| 554 |
#endif /* !GC_NO_THREADS_DISCOVERY */ |
| 555 |
|
| 556 |
/* Caller holds allocation lock. */ |
| 557 |
GC_INNER void GC_stop_world(void) |
| 558 |
{ |
| 559 |
task_t my_task = current_task(); |
| 560 |
mach_port_t my_thread = mach_thread_self(); |
| 561 |
kern_return_t kern_result; |
| 562 |
|
| 563 |
# ifdef DEBUG_THREADS |
| 564 |
GC_log_printf("Stopping the world from thread %p\n", |
| 565 |
(void *)(word)my_thread); |
| 566 |
# endif |
| 567 |
/* Make sure all free list construction has stopped before we */ |
| 568 |
/* start. No new construction can start, since free list */ |
| 569 |
/* construction is required to acquire and release the GC lock */ |
| 570 |
/* before it starts, and we have the lock. */ |
| 571 |
|
| 572 |
# ifdef PARALLEL_MARK |
| 573 |
if (GC_parallel) { |
| 574 |
GC_acquire_mark_lock(); |
| 575 |
GC_ASSERT(GC_fl_builder_count == 0); |
| 576 |
/* We should have previously waited for it to become zero. */ |
| 577 |
} |
| 578 |
# endif /* PARALLEL_MARK */ |
| 579 |
|
| 580 |
if (GC_query_task_threads) { |
| 581 |
# ifndef GC_NO_THREADS_DISCOVERY |
| 582 |
GC_bool changed; |
| 583 |
thread_act_array_t act_list, prev_list; |
| 584 |
mach_msg_type_number_t listcount, prevcount; |
| 585 |
|
| 586 |
/* Clear out the mach threads list table. We do not need to */ |
| 587 |
/* really clear GC_mach_threads[] as it is used only in the range */ |
| 588 |
/* from 0 to GC_mach_threads_count-1, inclusive. */ |
| 589 |
GC_mach_threads_count = 0; |
| 590 |
|
| 591 |
/* Loop stopping threads until you have gone over the whole list */ |
| 592 |
/* twice without a new one appearing. thread_create() won't */ |
| 593 |
/* return (and thus the thread stop) until the new thread exists, */ |
| 594 |
/* so there is no window whereby you could stop a thread, */ |
| 595 |
/* recognize it is stopped, but then have a new thread it created */ |
| 596 |
/* before stopping show up later. */ |
| 597 |
changed = TRUE; |
| 598 |
prev_list = NULL; |
| 599 |
prevcount = 0; |
| 600 |
do { |
| 601 |
kern_result = task_threads(my_task, &act_list, &listcount); |
| 602 |
|
| 603 |
if (kern_result == KERN_SUCCESS) { |
| 604 |
changed = GC_suspend_thread_list(act_list, listcount, prev_list, |
| 605 |
prevcount, my_task, my_thread); |
| 606 |
|
| 607 |
if (prev_list != NULL) { |
| 608 |
/* Thread ports are not deallocated by list, unused ports */ |
| 609 |
/* deallocated in GC_suspend_thread_list, used - kept in */ |
| 610 |
/* GC_mach_threads till GC_start_world as otherwise thread */ |
| 611 |
/* object change can occur and GC_start_world will not */ |
| 612 |
/* find the thread to resume which will cause app to hang. */ |
| 613 |
vm_deallocate(my_task, (vm_address_t)prev_list, |
| 614 |
sizeof(thread_t) * prevcount); |
| 615 |
} |
| 616 |
|
| 617 |
/* Repeat while having changes. */ |
| 618 |
prev_list = act_list; |
| 619 |
prevcount = listcount; |
| 620 |
} |
| 621 |
} while (changed); |
| 622 |
|
| 623 |
GC_ASSERT(prev_list != 0); |
| 624 |
/* The thread ports are not deallocated by list, see above. */ |
| 625 |
vm_deallocate(my_task, (vm_address_t)act_list, |
| 626 |
sizeof(thread_t) * listcount); |
| 627 |
# endif /* !GC_NO_THREADS_DISCOVERY */ |
| 628 |
|
| 629 |
} else { |
| 630 |
unsigned i; |
| 631 |
|
| 632 |
for (i = 0; i < THREAD_TABLE_SZ; i++) { |
| 633 |
GC_thread p; |
| 634 |
|
| 635 |
for (p = GC_threads[i]; p != NULL; p = p->next) { |
| 636 |
if ((p->flags & FINISHED) == 0 && !p->thread_blocked && |
| 637 |
p->stop_info.mach_thread != my_thread) { |
| 638 |
GC_acquire_dirty_lock(); |
| 639 |
do { |
| 640 |
kern_result = thread_suspend(p->stop_info.mach_thread); |
| 641 |
} while (kern_result == KERN_ABORTED); |
| 642 |
GC_release_dirty_lock(); |
| 643 |
if (kern_result != KERN_SUCCESS) |
| 644 |
ABORT("thread_suspend failed"); |
| 645 |
if (GC_on_thread_event) |
| 646 |
GC_on_thread_event(GC_EVENT_THREAD_SUSPENDED, |
| 647 |
(void *)(word)p->stop_info.mach_thread); |
| 648 |
} |
| 649 |
} |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
# ifdef MPROTECT_VDB |
| 654 |
if (GC_auto_incremental) { |
| 655 |
GC_mprotect_stop(); |
| 656 |
} |
| 657 |
# endif |
| 658 |
# ifdef PARALLEL_MARK |
| 659 |
if (GC_parallel) |
| 660 |
GC_release_mark_lock(); |
| 661 |
# endif |
| 662 |
|
| 663 |
# ifdef DEBUG_THREADS |
| 664 |
GC_log_printf("World stopped from %p\n", (void *)(word)my_thread); |
| 665 |
# endif |
| 666 |
mach_port_deallocate(my_task, my_thread); |
| 667 |
} |
| 668 |
|
| 669 |
GC_INLINE void GC_thread_resume(thread_act_t thread) |
| 670 |
{ |
| 671 |
kern_return_t kern_result; |
| 672 |
# if defined(DEBUG_THREADS) || defined(GC_ASSERTIONS) |
| 673 |
struct thread_basic_info info; |
| 674 |
mach_msg_type_number_t outCount = THREAD_BASIC_INFO_COUNT; |
| 675 |
|
| 676 |
# ifdef CPPCHECK |
| 677 |
info.run_state = 0; |
| 678 |
# endif |
| 679 |
kern_result = thread_info(thread, THREAD_BASIC_INFO, |
| 680 |
(thread_info_t)&info, &outCount); |
| 681 |
if (kern_result != KERN_SUCCESS) |
| 682 |
ABORT("thread_info failed"); |
| 683 |
# endif |
| 684 |
# ifdef DEBUG_THREADS |
| 685 |
GC_log_printf("Resuming thread %p with state %d\n", (void *)(word)thread, |
| 686 |
info.run_state); |
| 687 |
# endif |
| 688 |
/* Resume the thread */ |
| 689 |
kern_result = thread_resume(thread); |
| 690 |
if (kern_result != KERN_SUCCESS) { |
| 691 |
WARN("thread_resume(%p) failed: mach port invalid\n", thread); |
| 692 |
} else if (GC_on_thread_event) { |
| 693 |
GC_on_thread_event(GC_EVENT_THREAD_UNSUSPENDED, (void *)(word)thread); |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
/* Caller holds allocation lock, and has held it continuously since */ |
| 698 |
/* the world stopped. */ |
| 699 |
GC_INNER void GC_start_world(void) |
| 700 |
{ |
| 701 |
task_t my_task = current_task(); |
| 702 |
# ifdef DEBUG_THREADS |
| 703 |
GC_log_printf("World starting\n"); |
| 704 |
# endif |
| 705 |
# ifdef MPROTECT_VDB |
| 706 |
if (GC_auto_incremental) { |
| 707 |
GC_mprotect_resume(); |
| 708 |
} |
| 709 |
# endif |
| 710 |
|
| 711 |
if (GC_query_task_threads) { |
| 712 |
# ifndef GC_NO_THREADS_DISCOVERY |
| 713 |
int i, j; |
| 714 |
kern_return_t kern_result; |
| 715 |
thread_act_array_t act_list; |
| 716 |
mach_msg_type_number_t listcount; |
| 717 |
|
| 718 |
kern_result = task_threads(my_task, &act_list, &listcount); |
| 719 |
if (kern_result != KERN_SUCCESS) |
| 720 |
ABORT("task_threads failed"); |
| 721 |
|
| 722 |
j = (int)listcount; |
| 723 |
for (i = 0; i < GC_mach_threads_count; i++) { |
| 724 |
thread_act_t thread = GC_mach_threads[i].thread; |
| 725 |
|
| 726 |
if (GC_mach_threads[i].suspended) { |
| 727 |
int last_found = j; /* The thread index found during the */ |
| 728 |
/* previous iteration (count value */ |
| 729 |
/* means no thread found yet). */ |
| 730 |
|
| 731 |
/* Search for the thread starting from the last found one first. */ |
| 732 |
while (++j < (int)listcount) { |
| 733 |
if (act_list[j] == thread) |
| 734 |
break; |
| 735 |
} |
| 736 |
if (j >= (int)listcount) { |
| 737 |
/* If not found, search in the rest (beginning) of the list. */ |
| 738 |
for (j = 0; j < last_found; j++) { |
| 739 |
if (act_list[j] == thread) |
| 740 |
break; |
| 741 |
} |
| 742 |
} |
| 743 |
if (j != last_found) { |
| 744 |
/* The thread is alive, resume it. */ |
| 745 |
GC_thread_resume(thread); |
| 746 |
} |
| 747 |
} else { |
| 748 |
/* This thread was failed to be suspended by GC_stop_world, */ |
| 749 |
/* no action needed. */ |
| 750 |
# ifdef DEBUG_THREADS |
| 751 |
GC_log_printf("Not resuming thread %p as it is not suspended\n", |
| 752 |
(void *)(word)thread); |
| 753 |
# endif |
| 754 |
} |
| 755 |
mach_port_deallocate(my_task, thread); |
| 756 |
} |
| 757 |
|
| 758 |
for (i = 0; i < (int)listcount; i++) |
| 759 |
mach_port_deallocate(my_task, act_list[i]); |
| 760 |
vm_deallocate(my_task, (vm_address_t)act_list, |
| 761 |
sizeof(thread_t) * listcount); |
| 762 |
# endif /* !GC_NO_THREADS_DISCOVERY */ |
| 763 |
|
| 764 |
} else { |
| 765 |
int i; |
| 766 |
mach_port_t my_thread = mach_thread_self(); |
| 767 |
|
| 768 |
for (i = 0; i < THREAD_TABLE_SZ; i++) { |
| 769 |
GC_thread p; |
| 770 |
for (p = GC_threads[i]; p != NULL; p = p->next) { |
| 771 |
if ((p->flags & FINISHED) == 0 && !p->thread_blocked && |
| 772 |
p->stop_info.mach_thread != my_thread) |
| 773 |
GC_thread_resume(p->stop_info.mach_thread); |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
mach_port_deallocate(my_task, my_thread); |
| 778 |
} |
| 779 |
|
| 780 |
# ifdef DEBUG_THREADS |
| 781 |
GC_log_printf("World started\n"); |
| 782 |
# endif |
| 783 |
} |
| 784 |
|
| 785 |
#endif /* GC_DARWIN_THREADS */ |