1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2004
7 * This file is written in a subset of C--, extended with various
8 * features specific to GHC. It is compiled by GHC directly. For the
9 * syntax of .cmm files, see the parser in ghc/compiler/cmm/CmmParse.y.
11 * ---------------------------------------------------------------------------*/
14 #include "RaiseAsync.h"
16 /* -----------------------------------------------------------------------------
19 A thread can request that asynchronous exceptions not be delivered
20 ("blocked") for the duration of an I/O computation. The primitive
22 blockAsyncExceptions# :: IO a -> IO a
24 is used for this purpose. During a blocked section, asynchronous
25 exceptions may be unblocked again temporarily:
27 unblockAsyncExceptions# :: IO a -> IO a
29 Furthermore, asynchronous exceptions are blocked automatically during
30 the execution of an exception handler. Both of these primitives
31 leave a continuation on the stack which reverts to the previous
32 state (blocked or unblocked) on exit.
34 A thread which wants to raise an exception in another thread (using
35 killThread#) must block until the target thread is ready to receive
36 it. The action of unblocking exceptions in a thread will release all
37 the threads waiting to deliver exceptions to that thread.
39 NB. there's a bug in here. If a thread is inside an
40 unsafePerformIO, and inside blockAsyncExceptions# (there is an
41 unblockAsyncExceptions_ret on the stack), and it is blocked in an
42 interruptible operation, and it receives an exception, then the
43 unsafePerformIO thunk will be updated with a stack object
44 containing the unblockAsyncExceptions_ret frame. Later, when
45 someone else evaluates this thunk, the blocked exception state is
46 not restored, and the result is that unblockAsyncExceptions_ret
47 will attempt to unblock exceptions in the current thread, but it'll
48 find that the CurrentTSO->blocked_exceptions is NULL. Hence, we
49 work around this by checking for NULL in awakenBlockedQueue().
51 -------------------------------------------------------------------------- */
53 INFO_TABLE_RET( stg_unblockAsyncExceptionszh_ret,
54 0/*framesize*/, 0/*bitmap*/, RET_SMALL )
58 // Not true: see comments above
59 // ASSERT(StgTSO_blocked_exceptions(CurrentTSO) != NULL);
61 StgTSO_flags(CurrentTSO) = StgTSO_flags(CurrentTSO) &
62 ~(TSO_BLOCKEX::I32|TSO_INTERRUPTIBLE::I32);
64 /* Eagerly raise a blocked exception, if there is one */
65 if (StgTSO_blocked_exceptions(CurrentTSO) != END_TSO_QUEUE) {
67 * We have to be very careful here, as in killThread#, since
68 * we are about to raise an async exception in the current
69 * thread, which might result in the thread being killed.
74 * raiseAsync assumes that the stack is in ThreadRunGHC state,
75 * i.e. with a return address on the top. In unreg mode, the
76 * return value for IO is on top of the return address, so we
77 * need to make a small adjustment here.
82 r = foreign "C" maybePerformBlockedException (MyCapability() "ptr",
83 CurrentTSO "ptr") [R1];
86 if (StgTSO_what_next(CurrentTSO) == ThreadKilled::I16) {
91 ASSERT(StgTSO_what_next(CurrentTSO) == ThreadRunGHC::I16);
92 jump %ENTRY_CODE(Sp(0));
97 * Readjust stack in unregisterised mode if we didn't raise an
98 * exception, see above
108 jump %ENTRY_CODE(Sp(0));
112 jump %ENTRY_CODE(Sp(1));
116 INFO_TABLE_RET( stg_blockAsyncExceptionszh_ret,
117 0/*framesize*/, 0/*bitmap*/, RET_SMALL )
119 // Not true: see comments above
120 // ASSERT(StgTSO_blocked_exceptions(CurrentTSO) == NULL);
122 StgTSO_flags(CurrentTSO) =
123 StgTSO_flags(CurrentTSO) | TSO_BLOCKEX::I32 | TSO_INTERRUPTIBLE::I32;
127 jump %ENTRY_CODE(Sp(0));
131 jump %ENTRY_CODE(Sp(1));
135 blockAsyncExceptionszh_fast
137 /* Args: R1 :: IO a */
138 STK_CHK_GEN( WDS(2)/* worst case */, R1_PTR, blockAsyncExceptionszh_fast);
140 if ((TO_W_(StgTSO_flags(CurrentTSO)) & TSO_BLOCKEX) == 0) {
142 StgTSO_flags(CurrentTSO) =
143 StgTSO_flags(CurrentTSO) | TSO_BLOCKEX::I32 | TSO_INTERRUPTIBLE::I32;
145 /* avoid growing the stack unnecessarily */
146 if (Sp(0) == stg_blockAsyncExceptionszh_ret_info) {
150 Sp(0) = stg_unblockAsyncExceptionszh_ret_info;
158 unblockAsyncExceptionszh_fast
162 /* Args: R1 :: IO a */
163 STK_CHK_GEN( WDS(2), R1_PTR, unblockAsyncExceptionszh_fast);
165 if ((TO_W_(StgTSO_flags(CurrentTSO)) & TSO_BLOCKEX) != 0) {
167 StgTSO_flags(CurrentTSO) = StgTSO_flags(CurrentTSO) &
168 ~(TSO_BLOCKEX::I32|TSO_INTERRUPTIBLE::I32);
170 /* Eagerly raise a blocked exception, if there is one */
171 if (StgTSO_blocked_exceptions(CurrentTSO) != END_TSO_QUEUE) {
173 * We have to be very careful here, as in killThread#, since
174 * we are about to raise an async exception in the current
175 * thread, which might result in the thread being killed.
178 r = foreign "C" maybePerformBlockedException (MyCapability() "ptr",
179 CurrentTSO "ptr") [R1];
182 if (StgTSO_what_next(CurrentTSO) == ThreadKilled::I16) {
187 ASSERT(StgTSO_what_next(CurrentTSO) == ThreadRunGHC::I16);
188 jump %ENTRY_CODE(Sp(0));
193 /* avoid growing the stack unnecessarily */
194 if (Sp(0) == stg_unblockAsyncExceptionszh_ret_info) {
198 Sp(0) = stg_blockAsyncExceptionszh_ret_info;
209 /* args: R1 = TSO to kill, R2 = Exception */
218 STK_CHK_GEN( WDS(3), R1_PTR & R2_PTR, killThreadzh_fast);
221 * We might have killed ourselves. In which case, better be *very*
222 * careful. If the exception killed us, then return to the scheduler.
223 * If the exception went to a catch frame, we'll just continue from
226 if (target == CurrentTSO) {
228 /* ToDo: what if the current thread is blocking exceptions? */
229 foreign "C" throwToSingleThreaded(MyCapability() "ptr",
230 target "ptr", exception "ptr")[R1,R2];
231 if (StgTSO_what_next(CurrentTSO) == ThreadKilled::I16) {
236 ASSERT(StgTSO_what_next(CurrentTSO) == ThreadRunGHC::I16);
237 jump %ENTRY_CODE(Sp(0));
242 out = BaseReg + OFFSET_StgRegTable_rmp_tmp_w;
244 retcode = foreign "C" throwTo(MyCapability() "ptr",
250 switch [THROWTO_SUCCESS .. THROWTO_BLOCKED] (retcode) {
252 case THROWTO_SUCCESS: {
253 jump %ENTRY_CODE(Sp(0));
256 case THROWTO_BLOCKED: {
258 // we must block, and call throwToReleaseTarget() before returning
259 jump stg_block_throwto;
265 /* -----------------------------------------------------------------------------
267 -------------------------------------------------------------------------- */
270 #define CATCH_FRAME_ENTRY_TEMPLATE(label,ret) \
273 Sp = Sp + SIZEOF_StgCatchFrame; \
277 #define CATCH_FRAME_ENTRY_TEMPLATE(label,ret) \
282 Sp = Sp + SIZEOF_StgCatchFrame; \
294 CATCH_FRAME_ENTRY_TEMPLATE(stg_catch_frame_0_ret,%RET_VEC(Sp(SP_OFF),0))
295 CATCH_FRAME_ENTRY_TEMPLATE(stg_catch_frame_1_ret,%RET_VEC(Sp(SP_OFF),1))
296 CATCH_FRAME_ENTRY_TEMPLATE(stg_catch_frame_2_ret,%RET_VEC(Sp(SP_OFF),2))
297 CATCH_FRAME_ENTRY_TEMPLATE(stg_catch_frame_3_ret,%RET_VEC(Sp(SP_OFF),3))
298 CATCH_FRAME_ENTRY_TEMPLATE(stg_catch_frame_4_ret,%RET_VEC(Sp(SP_OFF),4))
299 CATCH_FRAME_ENTRY_TEMPLATE(stg_catch_frame_5_ret,%RET_VEC(Sp(SP_OFF),5))
300 CATCH_FRAME_ENTRY_TEMPLATE(stg_catch_frame_6_ret,%RET_VEC(Sp(SP_OFF),6))
301 CATCH_FRAME_ENTRY_TEMPLATE(stg_catch_frame_7_ret,%RET_VEC(Sp(SP_OFF),7))
303 #if MAX_VECTORED_RTN > 8
304 #error MAX_VECTORED_RTN has changed: please modify stg_catch_frame too.
307 #if defined(PROFILING)
308 #define CATCH_FRAME_BITMAP 7
309 #define CATCH_FRAME_WORDS 4
311 #define CATCH_FRAME_BITMAP 1
312 #define CATCH_FRAME_WORDS 2
315 /* Catch frames are very similar to update frames, but when entering
316 * one we just pop the frame off the stack and perform the correct
317 * kind of return to the activation record underneath us on the stack.
320 INFO_TABLE_RET(stg_catch_frame,
321 CATCH_FRAME_WORDS, CATCH_FRAME_BITMAP,
323 stg_catch_frame_0_ret,
324 stg_catch_frame_1_ret,
325 stg_catch_frame_2_ret,
326 stg_catch_frame_3_ret,
327 stg_catch_frame_4_ret,
328 stg_catch_frame_5_ret,
329 stg_catch_frame_6_ret,
330 stg_catch_frame_7_ret)
331 CATCH_FRAME_ENTRY_TEMPLATE(,%ENTRY_CODE(Sp(SP_OFF)))
333 /* -----------------------------------------------------------------------------
334 * The catch infotable
336 * This should be exactly the same as would be generated by this STG code
338 * catch = {x,h} \n {} -> catch#{x,h}
340 * It is used in deleteThread when reverting blackholes.
341 * -------------------------------------------------------------------------- */
343 INFO_TABLE(stg_catch,2,0,FUN,"catch","catch")
345 R2 = StgClosure_payload(R1,1); /* h */
346 R1 = StgClosure_payload(R1,0); /* x */
352 /* args: R1 = m :: IO a, R2 = handler :: Exception -> IO a */
353 STK_CHK_GEN(SIZEOF_StgCatchFrame + WDS(1), R1_PTR & R2_PTR, catchzh_fast);
355 /* Set up the catch frame */
356 Sp = Sp - SIZEOF_StgCatchFrame;
357 SET_HDR(Sp,stg_catch_frame_info,W_[CCCS]);
359 StgCatchFrame_handler(Sp) = R2;
360 StgCatchFrame_exceptions_blocked(Sp) = TO_W_(StgTSO_flags(CurrentTSO)) & TSO_BLOCKEX;
361 TICK_CATCHF_PUSHED();
363 /* Apply R1 to the realworld token */
369 /* -----------------------------------------------------------------------------
370 * The raise infotable
372 * This should be exactly the same as would be generated by this STG code
374 * raise = {err} \n {} -> raise#{err}
376 * It is used in raisezh_fast to update thunks on the update list
377 * -------------------------------------------------------------------------- */
379 INFO_TABLE(stg_raise,1,0,THUNK_1_0,"raise","raise")
381 R1 = StgThunk_payload(R1,0);
390 /* args : R1 :: Exception */
393 #if defined(PROFILING)
394 /* Debugging tool: on raising an exception, show where we are. */
396 /* ToDo: currently this is a hack. Would be much better if
397 * the info was only displayed for an *uncaught* exception.
399 if (RtsFlags_ProfFlags_showCCSOnException(RtsFlags) != 0::I32) {
400 foreign "C" fprintCCS_stderr(W_[CCCS] "ptr");
404 /* Inform the Hpc that an exception has been thrown */
405 foreign "C" hs_hpc_raise_event(CurrentTSO "ptr");
408 StgTSO_sp(CurrentTSO) = Sp;
409 frame_type = foreign "C" raiseExceptionHelper(BaseReg "ptr", CurrentTSO "ptr", R1 "ptr");
410 Sp = StgTSO_sp(CurrentTSO);
411 if (frame_type == ATOMICALLY_FRAME) {
412 /* The exception has reached the edge of a memory transaction. Check that
413 * the transaction is valid. If not then perhaps the exception should
414 * not have been thrown: re-run the transaction. "trec" will either be
415 * a top-level transaction running the atomic block, or a nested
416 * transaction running an invariant check. In the latter case we
417 * abort and de-allocate the top-level transaction that encloses it
418 * as well (we could just abandon its transaction record, but this makes
419 * sure it's marked as aborted and available for re-use). */
422 trec = StgTSO_trec(CurrentTSO);
423 r = foreign "C" stmValidateNestOfTransactions(trec "ptr");
424 "ptr" outer = foreign "C" stmGetEnclosingTRec(trec "ptr") [];
425 foreign "C" stmAbortTransaction(MyCapability() "ptr", trec "ptr");
426 foreign "C" stmFreeAbortedTRec(MyCapability() "ptr", trec "ptr");
428 if (outer != NO_TREC) {
429 foreign "C" stmAbortTransaction(MyCapability() "ptr", outer "ptr");
430 foreign "C" stmFreeAbortedTRec(MyCapability() "ptr", outer "ptr");
433 StgTSO_trec(CurrentTSO) = NO_TREC;
435 // Transaction was valid: continue searching for a catch frame
436 Sp = Sp + SIZEOF_StgAtomicallyFrame;
437 goto retry_pop_stack;
439 // Transaction was not valid: we retry the exception (otherwise continue
440 // with a further call to raiseExceptionHelper)
441 "ptr" trec = foreign "C" stmStartTransaction(MyCapability() "ptr", NO_TREC "ptr");
442 StgTSO_trec(CurrentTSO) = trec;
443 R1 = StgAtomicallyFrame_code(Sp);
448 if (frame_type == STOP_FRAME) {
450 * We've stripped the entire stack, the thread is now dead.
451 * We will leave the stack in a GC'able state, see the stg_stop_thread
452 * entry code in StgStartup.cmm.
454 Sp = CurrentTSO + TSO_OFFSET_StgTSO_stack
455 + WDS(TO_W_(StgTSO_stack_size(CurrentTSO))) - WDS(2);
456 Sp(1) = R1; /* save the exception */
457 Sp(0) = stg_enter_info; /* so that GC can traverse this stack */
458 StgTSO_what_next(CurrentTSO) = ThreadKilled::I16;
459 SAVE_THREAD_STATE(); /* inline! */
461 /* The return code goes in BaseReg->rRet, and BaseReg is returned in R1 */
462 StgRegTable_rRet(BaseReg) = ThreadFinished;
468 /* Ok, Sp points to the enclosing CATCH_FRAME or CATCH_STM_FRAME. Pop everything
469 * down to and including this frame, update Su, push R1, and enter the handler.
471 if (frame_type == CATCH_FRAME) {
472 handler = StgCatchFrame_handler(Sp);
474 handler = StgCatchSTMFrame_handler(Sp);
477 /* Restore the blocked/unblocked state for asynchronous exceptions
478 * at the CATCH_FRAME.
480 * If exceptions were unblocked, arrange that they are unblocked
481 * again after executing the handler by pushing an
482 * unblockAsyncExceptions_ret stack frame.
484 * If we've reached an STM catch frame then roll back the nested
485 * transaction we were using.
489 if (frame_type == CATCH_FRAME) {
490 Sp = Sp + SIZEOF_StgCatchFrame;
491 if (StgCatchFrame_exceptions_blocked(frame) == 0) {
493 Sp(0) = stg_unblockAsyncExceptionszh_ret_info;
497 trec = StgTSO_trec(CurrentTSO);
498 "ptr" outer = foreign "C" stmGetEnclosingTRec(trec "ptr") [];
499 foreign "C" stmAbortTransaction(MyCapability() "ptr", trec "ptr") [];
500 foreign "C" stmFreeAbortedTRec(MyCapability() "ptr", trec "ptr") [];
501 StgTSO_trec(CurrentTSO) = outer;
502 Sp = Sp + SIZEOF_StgCatchSTMFrame;
505 /* Ensure that async excpetions are blocked when running the handler.
507 StgTSO_flags(CurrentTSO) =
508 StgTSO_flags(CurrentTSO) | TSO_BLOCKEX::I32 | TSO_INTERRUPTIBLE::I32;
510 /* Call the handler, passing the exception value and a realworld
511 * token as arguments.
519 jump RET_LBL(stg_ap_pv);
524 /* Args :: R1 :: Exception */