darwin_stop_world.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. #include "private/pthread_support.h"
  2. /* This probably needs more porting work to ppc64. */
  3. # if defined(GC_DARWIN_THREADS)
  4. /* From "Inside Mac OS X - Mach-O Runtime Architecture" published by Apple
  5. Page 49:
  6. "The space beneath the stack pointer, where a new stack frame would normally
  7. be allocated, is called the red zone. This area as shown in Figure 3-2 may
  8. be used for any purpose as long as a new stack frame does not need to be
  9. added to the stack."
  10. Page 50: "If a leaf procedure's red zone usage would exceed 224 bytes, then
  11. it must set up a stack frame just like routines that call other routines."
  12. */
  13. #if defined(__ppc__)
  14. # define PPC_RED_ZONE_SIZE 224
  15. #elif defined(__ppc64__)
  16. # define PPC_RED_ZONE_SIZE 320
  17. #endif
  18. typedef struct StackFrame {
  19. unsigned long savedSP;
  20. unsigned long savedCR;
  21. unsigned long savedLR;
  22. unsigned long reserved[2];
  23. unsigned long savedRTOC;
  24. } StackFrame;
  25. unsigned long FindTopOfStack(unsigned long stack_start) {
  26. StackFrame *frame;
  27. if (stack_start == 0) {
  28. # ifdef POWERPC
  29. # if CPP_WORDSZ == 32
  30. __asm__ volatile("lwz %0,0(r1)" : "=r" (frame));
  31. # else
  32. __asm__ volatile("ld %0,0(r1)" : "=r" (frame));
  33. # endif
  34. # endif
  35. } else {
  36. frame = (StackFrame *)stack_start;
  37. }
  38. # ifdef DEBUG_THREADS
  39. /* GC_printf1("FindTopOfStack start at sp = %p\n", frame); */
  40. # endif
  41. do {
  42. if (frame->savedSP == 0) break;
  43. /* if there are no more stack frames, stop */
  44. frame = (StackFrame*)frame->savedSP;
  45. /* we do these next two checks after going to the next frame
  46. because the LR for the first stack frame in the loop
  47. is not set up on purpose, so we shouldn't check it. */
  48. if ((frame->savedLR & ~3) == 0) break; /* if the next LR is bogus, stop */
  49. if ((~(frame->savedLR) & ~3) == 0) break; /* ditto */
  50. } while (1);
  51. # ifdef DEBUG_THREADS
  52. /* GC_printf1("FindTopOfStack finish at sp = %p\n", frame); */
  53. # endif
  54. return (unsigned long)frame;
  55. }
  56. #ifdef DARWIN_DONT_PARSE_STACK
  57. void GC_push_all_stacks() {
  58. int i;
  59. kern_return_t r;
  60. GC_thread p;
  61. pthread_t me;
  62. ptr_t lo, hi;
  63. GC_THREAD_STATE_T state;
  64. mach_msg_type_number_t thread_state_count = GC_MACH_THREAD_STATE_COUNT;
  65. me = pthread_self();
  66. if (!GC_thr_initialized) GC_thr_init();
  67. for(i=0;i<THREAD_TABLE_SZ;i++) {
  68. for(p=GC_threads[i];p!=0;p=p->next) {
  69. if(p -> flags & FINISHED) continue;
  70. if(pthread_equal(p->id,me)) {
  71. lo = GC_approx_sp();
  72. } else {
  73. /* Get the thread state (registers, etc) */
  74. r = thread_get_state(p->stop_info.mach_thread, GC_MACH_THREAD_STATE,
  75. (natural_t*)&state, &thread_state_count);
  76. if(r != KERN_SUCCESS) ABORT("thread_get_state failed");
  77. #if defined(I386)
  78. lo = (void*)state . THREAD_FLD (esp);
  79. GC_push_one(state . THREAD_FLD (eax));
  80. GC_push_one(state . THREAD_FLD (ebx));
  81. GC_push_one(state . THREAD_FLD (ecx));
  82. GC_push_one(state . THREAD_FLD (edx));
  83. GC_push_one(state . THREAD_FLD (edi));
  84. GC_push_one(state . THREAD_FLD (esi));
  85. GC_push_one(state . THREAD_FLD (ebp));
  86. #elif defined(X86_64)
  87. lo = (void*)state . THREAD_FLD (rsp);
  88. GC_push_one(state . THREAD_FLD (rax));
  89. GC_push_one(state . THREAD_FLD (rbx));
  90. GC_push_one(state . THREAD_FLD (rcx));
  91. GC_push_one(state . THREAD_FLD (rdx));
  92. GC_push_one(state . THREAD_FLD (rdi));
  93. GC_push_one(state . THREAD_FLD (rsi));
  94. GC_push_one(state . THREAD_FLD (rbp));
  95. GC_push_one(state . THREAD_FLD (rsp));
  96. GC_push_one(state . THREAD_FLD (r8));
  97. GC_push_one(state . THREAD_FLD (r9));
  98. GC_push_one(state . THREAD_FLD (r10));
  99. GC_push_one(state . THREAD_FLD (r11));
  100. GC_push_one(state . THREAD_FLD (r12));
  101. GC_push_one(state . THREAD_FLD (r13));
  102. GC_push_one(state . THREAD_FLD (r14));
  103. GC_push_one(state . THREAD_FLD (r15));
  104. GC_push_one(state . THREAD_FLD (rip));
  105. GC_push_one(state . THREAD_FLD (rflags));
  106. GC_push_one(state . THREAD_FLD (cs));
  107. GC_push_one(state . THREAD_FLD (fs));
  108. GC_push_one(state . THREAD_FLD (gs));
  109. #elif defined(POWERPC)
  110. lo = (void*)(state . THREAD_FLD (r1) - PPC_RED_ZONE_SIZE);
  111. GC_push_one(state . THREAD_FLD (r0));
  112. GC_push_one(state . THREAD_FLD (r2));
  113. GC_push_one(state . THREAD_FLD (r3));
  114. GC_push_one(state . THREAD_FLD (r4));
  115. GC_push_one(state . THREAD_FLD (r5));
  116. GC_push_one(state . THREAD_FLD (r6));
  117. GC_push_one(state . THREAD_FLD (r7));
  118. GC_push_one(state . THREAD_FLD (r8));
  119. GC_push_one(state . THREAD_FLD (r9));
  120. GC_push_one(state . THREAD_FLD (r10));
  121. GC_push_one(state . THREAD_FLD (r11));
  122. GC_push_one(state . THREAD_FLD (r12));
  123. GC_push_one(state . THREAD_FLD (r13));
  124. GC_push_one(state . THREAD_FLD (r14));
  125. GC_push_one(state . THREAD_FLD (r15));
  126. GC_push_one(state . THREAD_FLD (r16));
  127. GC_push_one(state . THREAD_FLD (r17));
  128. GC_push_one(state . THREAD_FLD (r18));
  129. GC_push_one(state . THREAD_FLD (r19));
  130. GC_push_one(state . THREAD_FLD (r20));
  131. GC_push_one(state . THREAD_FLD (r21));
  132. GC_push_one(state . THREAD_FLD (r22));
  133. GC_push_one(state . THREAD_FLD (r23));
  134. GC_push_one(state . THREAD_FLD (r24));
  135. GC_push_one(state . THREAD_FLD (r25));
  136. GC_push_one(state . THREAD_FLD (r26));
  137. GC_push_one(state . THREAD_FLD (r27));
  138. GC_push_one(state . THREAD_FLD (r28));
  139. GC_push_one(state . THREAD_FLD (r29));
  140. GC_push_one(state . THREAD_FLD (r30));
  141. GC_push_one(state . THREAD_FLD (r31));
  142. #else
  143. # error FIXME for non-x86 || ppc architectures
  144. #endif
  145. } /* p != me */
  146. if(p->flags & MAIN_THREAD)
  147. hi = GC_stackbottom;
  148. else
  149. hi = p->stack_end;
  150. #if DEBUG_THREADS
  151. GC_printf3("Darwin: Stack for thread 0x%lx = [%lx,%lx)\n",
  152. (unsigned long) p -> id,
  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. } /* for(i=0;i<THREAD_TABLE_SZ...) */
  160. }
  161. #else /* !DARWIN_DONT_PARSE_STACK; Use FindTopOfStack() */
  162. void GC_push_all_stacks() {
  163. int i;
  164. kern_return_t r;
  165. mach_port_t me;
  166. ptr_t lo, hi;
  167. thread_act_array_t act_list = 0;
  168. mach_msg_type_number_t listcount = 0;
  169. me = mach_thread_self();
  170. if (!GC_thr_initialized) GC_thr_init();
  171. r = task_threads(current_task(), &act_list, &listcount);
  172. if(r != KERN_SUCCESS) ABORT("task_threads failed");
  173. for(i = 0; i < listcount; i++) {
  174. thread_act_t thread = act_list[i];
  175. if (thread == me) {
  176. lo = GC_approx_sp();
  177. hi = (ptr_t)FindTopOfStack(0);
  178. } else {
  179. # if defined(__ppc__) || defined(__ppc64__)
  180. GC_THREAD_STATE_T info;
  181. mach_msg_type_number_t outCount = THREAD_STATE_MAX;
  182. r = thread_get_state(thread, GC_MACH_THREAD_STATE,
  183. (natural_t *)&info, &outCount);
  184. if(r != KERN_SUCCESS) ABORT("task_get_state failed");
  185. lo = (void*)(info . THREAD_FLD (r1) - PPC_RED_ZONE_SIZE);
  186. hi = (ptr_t)FindTopOfStack(info . THREAD_FLD (r1));
  187. GC_push_one(info . THREAD_FLD (r0));
  188. GC_push_one(info . THREAD_FLD (r2));
  189. GC_push_one(info . THREAD_FLD (r3));
  190. GC_push_one(info . THREAD_FLD (r4));
  191. GC_push_one(info . THREAD_FLD (r5));
  192. GC_push_one(info . THREAD_FLD (r6));
  193. GC_push_one(info . THREAD_FLD (r7));
  194. GC_push_one(info . THREAD_FLD (r8));
  195. GC_push_one(info . THREAD_FLD (r9));
  196. GC_push_one(info . THREAD_FLD (r10));
  197. GC_push_one(info . THREAD_FLD (r11));
  198. GC_push_one(info . THREAD_FLD (r12));
  199. GC_push_one(info . THREAD_FLD (r13));
  200. GC_push_one(info . THREAD_FLD (r14));
  201. GC_push_one(info . THREAD_FLD (r15));
  202. GC_push_one(info . THREAD_FLD (r16));
  203. GC_push_one(info . THREAD_FLD (r17));
  204. GC_push_one(info . THREAD_FLD (r18));
  205. GC_push_one(info . THREAD_FLD (r19));
  206. GC_push_one(info . THREAD_FLD (r20));
  207. GC_push_one(info . THREAD_FLD (r21));
  208. GC_push_one(info . THREAD_FLD (r22));
  209. GC_push_one(info . THREAD_FLD (r23));
  210. GC_push_one(info . THREAD_FLD (r24));
  211. GC_push_one(info . THREAD_FLD (r25));
  212. GC_push_one(info . THREAD_FLD (r26));
  213. GC_push_one(info . THREAD_FLD (r27));
  214. GC_push_one(info . THREAD_FLD (r28));
  215. GC_push_one(info . THREAD_FLD (r29));
  216. GC_push_one(info . THREAD_FLD (r30));
  217. GC_push_one(info . THREAD_FLD (r31));
  218. # else
  219. /* FIXME: Remove after testing: */
  220. WARN("This is completely untested and likely will not work\n", 0);
  221. GC_THREAD_STATE_T info;
  222. mach_msg_type_number_t outCount = THREAD_STATE_MAX;
  223. r = thread_get_state(thread, GC_MACH_THREAD_STATE, (natural_t *)&info,
  224. &outCount);
  225. if(r != KERN_SUCCESS) ABORT("task_get_state failed");
  226. lo = (void*)info . THREAD_FLD (esp);
  227. hi = (ptr_t)FindTopOfStack(info . THREAD_FLD (esp));
  228. GC_push_one(info . THREAD_FLD (eax));
  229. GC_push_one(info . THREAD_FLD (ebx));
  230. GC_push_one(info . THREAD_FLD (ecx));
  231. GC_push_one(info . THREAD_FLD (edx));
  232. GC_push_one(info . THREAD_FLD (edi));
  233. GC_push_one(info . THREAD_FLD (esi));
  234. /* GC_push_one(info . THREAD_FLD (ebp)); */
  235. /* GC_push_one(info . THREAD_FLD (esp)); */
  236. GC_push_one(info . THREAD_FLD (ss));
  237. GC_push_one(info . THREAD_FLD (eip));
  238. GC_push_one(info . THREAD_FLD (cs));
  239. GC_push_one(info . THREAD_FLD (ds));
  240. GC_push_one(info . THREAD_FLD (es));
  241. GC_push_one(info . THREAD_FLD (fs));
  242. GC_push_one(info . THREAD_FLD (gs));
  243. # endif /* !POWERPC */
  244. }
  245. # if DEBUG_THREADS
  246. GC_printf3("Darwin: Stack for thread 0x%lx = [%lx,%lx)\n",
  247. (unsigned long) thread,
  248. (unsigned long) lo,
  249. (unsigned long) hi
  250. );
  251. # endif
  252. GC_push_all_stack(lo, hi);
  253. } /* for(p=GC_threads[i]...) */
  254. vm_deallocate(current_task(), (vm_address_t)act_list, sizeof(thread_t) * listcount);
  255. }
  256. #endif /* !DARWIN_DONT_PARSE_STACK */
  257. static mach_port_t GC_mach_handler_thread;
  258. static int GC_use_mach_handler_thread = 0;
  259. static struct GC_mach_thread GC_mach_threads[THREAD_TABLE_SZ];
  260. static int GC_mach_threads_count;
  261. void GC_stop_init() {
  262. int i;
  263. for (i = 0; i < THREAD_TABLE_SZ; i++) {
  264. GC_mach_threads[i].thread = 0;
  265. GC_mach_threads[i].already_suspended = 0;
  266. }
  267. GC_mach_threads_count = 0;
  268. }
  269. /* returns true if there's a thread in act_list that wasn't in old_list */
  270. int GC_suspend_thread_list(thread_act_array_t act_list, int count,
  271. thread_act_array_t old_list, int old_count) {
  272. mach_port_t my_thread = mach_thread_self();
  273. int i, j;
  274. int changed = 0;
  275. for(i = 0; i < count; i++) {
  276. thread_act_t thread = act_list[i];
  277. # if DEBUG_THREADS
  278. GC_printf1("Attempting to suspend thread %p\n", thread);
  279. # endif
  280. /* find the current thread in the old list */
  281. int found = 0;
  282. for(j = 0; j < old_count; j++) {
  283. thread_act_t old_thread = old_list[j];
  284. if (old_thread == thread) {
  285. found = 1;
  286. break;
  287. }
  288. }
  289. if (!found) {
  290. /* add it to the GC_mach_threads list */
  291. GC_mach_threads[GC_mach_threads_count].thread = thread;
  292. /* default is not suspended */
  293. GC_mach_threads[GC_mach_threads_count].already_suspended = 0;
  294. changed = 1;
  295. }
  296. if (thread != my_thread &&
  297. (!GC_use_mach_handler_thread
  298. || (GC_use_mach_handler_thread
  299. && GC_mach_handler_thread != thread))) {
  300. struct thread_basic_info info;
  301. mach_msg_type_number_t outCount = THREAD_INFO_MAX;
  302. kern_return_t kern_result = thread_info(thread, THREAD_BASIC_INFO,
  303. (thread_info_t)&info, &outCount);
  304. if(kern_result != KERN_SUCCESS) {
  305. /* the thread may have quit since the thread_threads () call
  306. * we mark already_suspended so it's not dealt with anymore later
  307. */
  308. if (!found) {
  309. GC_mach_threads[GC_mach_threads_count].already_suspended = TRUE;
  310. GC_mach_threads_count++;
  311. }
  312. continue;
  313. }
  314. # if DEBUG_THREADS
  315. GC_printf2("Thread state for 0x%lx = %d\n", thread, info.run_state);
  316. # endif
  317. if (!found) {
  318. GC_mach_threads[GC_mach_threads_count].already_suspended = info.suspend_count;
  319. }
  320. if (info.suspend_count) continue;
  321. # if DEBUG_THREADS
  322. GC_printf1("Suspending 0x%lx\n", thread);
  323. # endif
  324. /* Suspend the thread */
  325. kern_result = thread_suspend(thread);
  326. if(kern_result != KERN_SUCCESS) {
  327. /* the thread may have quit since the thread_threads () call
  328. * we mark already_suspended so it's not dealt with anymore later
  329. */
  330. if (!found) {
  331. GC_mach_threads[GC_mach_threads_count].already_suspended = TRUE;
  332. GC_mach_threads_count++;
  333. }
  334. continue;
  335. }
  336. }
  337. if (!found) GC_mach_threads_count++;
  338. }
  339. return changed;
  340. }
  341. /* Caller holds allocation lock. */
  342. void GC_stop_world()
  343. {
  344. int i, changes;
  345. GC_thread p;
  346. mach_port_t my_thread = mach_thread_self();
  347. kern_return_t kern_result;
  348. thread_act_array_t act_list, prev_list;
  349. mach_msg_type_number_t listcount, prevcount;
  350. # if DEBUG_THREADS
  351. GC_printf1("Stopping the world from 0x%lx\n", mach_thread_self());
  352. # endif
  353. /* clear out the mach threads list table */
  354. GC_stop_init();
  355. /* Make sure all free list construction has stopped before we start. */
  356. /* No new construction can start, since free list construction is */
  357. /* required to acquire and release the GC lock before it starts, */
  358. /* and we have the lock. */
  359. # ifdef PARALLEL_MARK
  360. GC_acquire_mark_lock();
  361. GC_ASSERT(GC_fl_builder_count == 0);
  362. /* We should have previously waited for it to become zero. */
  363. # endif /* PARALLEL_MARK */
  364. /* Loop stopping threads until you have gone over the whole list
  365. twice without a new one appearing. thread_create() won't
  366. return (and thus the thread stop) until the new thread
  367. exists, so there is no window whereby you could stop a
  368. thread, recognise it is stopped, but then have a new thread
  369. it created before stopping show up later.
  370. */
  371. changes = 1;
  372. prev_list = NULL;
  373. prevcount = 0;
  374. do {
  375. int result;
  376. kern_result = task_threads(current_task(), &act_list, &listcount);
  377. result = GC_suspend_thread_list(act_list, listcount,
  378. prev_list, prevcount);
  379. changes = result;
  380. prev_list = act_list;
  381. prevcount = listcount;
  382. vm_deallocate(current_task(), (vm_address_t)act_list, sizeof(thread_t) * listcount);
  383. } while (changes);
  384. # ifdef MPROTECT_VDB
  385. if(GC_incremental) {
  386. extern void GC_mprotect_stop();
  387. GC_mprotect_stop();
  388. }
  389. # endif
  390. # ifdef PARALLEL_MARK
  391. GC_release_mark_lock();
  392. # endif
  393. #if DEBUG_THREADS
  394. GC_printf1("World stopped from 0x%lx\n", my_thread);
  395. #endif
  396. }
  397. /* Caller holds allocation lock, and has held it continuously since */
  398. /* the world stopped. */
  399. void GC_start_world()
  400. {
  401. mach_port_t my_thread = mach_thread_self();
  402. int i, j;
  403. GC_thread p;
  404. kern_return_t kern_result;
  405. thread_act_array_t act_list;
  406. mach_msg_type_number_t listcount;
  407. struct thread_basic_info info;
  408. mach_msg_type_number_t outCount = THREAD_INFO_MAX;
  409. # if DEBUG_THREADS
  410. GC_printf0("World starting\n");
  411. # endif
  412. # ifdef MPROTECT_VDB
  413. if(GC_incremental) {
  414. extern void GC_mprotect_resume();
  415. GC_mprotect_resume();
  416. }
  417. # endif
  418. kern_result = task_threads(current_task(), &act_list, &listcount);
  419. for(i = 0; i < listcount; i++) {
  420. thread_act_t thread = act_list[i];
  421. if (thread != my_thread &&
  422. (!GC_use_mach_handler_thread ||
  423. (GC_use_mach_handler_thread && GC_mach_handler_thread != thread))) {
  424. for(j = 0; j < GC_mach_threads_count; j++) {
  425. if (thread == GC_mach_threads[j].thread) {
  426. if (GC_mach_threads[j].already_suspended) {
  427. # if DEBUG_THREADS
  428. GC_printf1("Not resuming already suspended thread %p\n", thread);
  429. # endif
  430. continue;
  431. }
  432. kern_result = thread_info(thread, THREAD_BASIC_INFO,
  433. (thread_info_t)&info, &outCount);
  434. if(kern_result != KERN_SUCCESS) ABORT("thread_info failed");
  435. # if DEBUG_THREADS
  436. GC_printf2("Thread state for 0x%lx = %d\n", thread,
  437. info.run_state);
  438. GC_printf1("Resuming 0x%lx\n", thread);
  439. # endif
  440. /* Resume the thread */
  441. kern_result = thread_resume(thread);
  442. if(kern_result != KERN_SUCCESS) ABORT("thread_resume failed");
  443. }
  444. }
  445. }
  446. }
  447. vm_deallocate(current_task(), (vm_address_t)act_list, sizeof(thread_t) * listcount);
  448. # if DEBUG_THREADS
  449. GC_printf0("World started\n");
  450. # endif
  451. }
  452. void GC_darwin_register_mach_handler_thread(mach_port_t thread) {
  453. GC_mach_handler_thread = thread;
  454. GC_use_mach_handler_thread = 1;
  455. }
  456. #endif