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
48 -------------------------------------------------------------------------- */
50 INFO_TABLE_RET( stg_unblockAsyncExceptionszh_ret, RET_SMALL )
54 StgTSO_flags(CurrentTSO) = StgTSO_flags(CurrentTSO) &
55 ~(TSO_BLOCKEX::I32|TSO_INTERRUPTIBLE::I32);
57 /* Eagerly raise a blocked exception, if there is one */
58 if (StgTSO_blocked_exceptions(CurrentTSO) != END_TSO_QUEUE) {
60 * We have to be very careful here, as in killThread#, since
61 * we are about to raise an async exception in the current
62 * thread, which might result in the thread being killed.
67 * raiseAsync assumes that the stack is in ThreadRunGHC state,
68 * i.e. with a return address on the top. In unreg mode, the
69 * return value for IO is on top of the return address, so we
70 * need to make a small adjustment here.
75 (r) = foreign "C" maybePerformBlockedException (MyCapability() "ptr",
76 CurrentTSO "ptr") [R1];
79 if (StgTSO_what_next(CurrentTSO) == ThreadKilled::I16) {
80 jump stg_threadFinished;
83 ASSERT(StgTSO_what_next(CurrentTSO) == ThreadRunGHC::I16);
84 jump %ENTRY_CODE(Sp(0));
89 * Readjust stack in unregisterised mode if we didn't raise an
90 * exception, see above
100 jump %ENTRY_CODE(Sp(0));
104 jump %ENTRY_CODE(Sp(1));
108 INFO_TABLE_RET( stg_blockAsyncExceptionszh_ret, RET_SMALL )
110 StgTSO_flags(CurrentTSO) =
111 StgTSO_flags(CurrentTSO) | TSO_BLOCKEX::I32 | TSO_INTERRUPTIBLE::I32;
115 jump %ENTRY_CODE(Sp(0));
119 jump %ENTRY_CODE(Sp(1));
123 blockAsyncExceptionszh_fast
125 /* Args: R1 :: IO a */
126 STK_CHK_GEN( WDS(2)/* worst case */, R1_PTR, blockAsyncExceptionszh_fast);
128 if ((TO_W_(StgTSO_flags(CurrentTSO)) & TSO_BLOCKEX) == 0) {
130 StgTSO_flags(CurrentTSO) =
131 StgTSO_flags(CurrentTSO) | TSO_BLOCKEX::I32 | TSO_INTERRUPTIBLE::I32;
133 /* avoid growing the stack unnecessarily */
134 if (Sp(0) == stg_blockAsyncExceptionszh_ret_info) {
138 Sp(0) = stg_unblockAsyncExceptionszh_ret_info;
146 unblockAsyncExceptionszh_fast
150 /* Args: R1 :: IO a */
151 STK_CHK_GEN( WDS(2), R1_PTR, unblockAsyncExceptionszh_fast);
153 if ((TO_W_(StgTSO_flags(CurrentTSO)) & TSO_BLOCKEX) != 0) {
155 StgTSO_flags(CurrentTSO) = StgTSO_flags(CurrentTSO) &
156 ~(TSO_BLOCKEX::I32|TSO_INTERRUPTIBLE::I32);
158 /* Eagerly raise a blocked exception, if there is one */
159 if (StgTSO_blocked_exceptions(CurrentTSO) != END_TSO_QUEUE) {
161 * We have to be very careful here, as in killThread#, since
162 * we are about to raise an async exception in the current
163 * thread, which might result in the thread being killed.
166 (r) = foreign "C" maybePerformBlockedException (MyCapability() "ptr",
167 CurrentTSO "ptr") [R1];
170 if (StgTSO_what_next(CurrentTSO) == ThreadKilled::I16) {
171 jump stg_threadFinished;
174 ASSERT(StgTSO_what_next(CurrentTSO) == ThreadRunGHC::I16);
175 jump %ENTRY_CODE(Sp(0));
180 /* avoid growing the stack unnecessarily */
181 if (Sp(0) == stg_unblockAsyncExceptionszh_ret_info) {
185 Sp(0) = stg_blockAsyncExceptionszh_ret_info;
196 /* args: R1 = TSO to kill, R2 = Exception */
205 STK_CHK_GEN( WDS(3), R1_PTR & R2_PTR, killThreadzh_fast);
208 * We might have killed ourselves. In which case, better be *very*
209 * careful. If the exception killed us, then return to the scheduler.
210 * If the exception went to a catch frame, we'll just continue from
213 if (target == CurrentTSO) {
215 /* ToDo: what if the current thread is blocking exceptions? */
216 foreign "C" throwToSingleThreaded(MyCapability() "ptr",
217 target "ptr", exception "ptr")[R1,R2];
218 if (StgTSO_what_next(CurrentTSO) == ThreadKilled::I16) {
219 jump stg_threadFinished;
222 ASSERT(StgTSO_what_next(CurrentTSO) == ThreadRunGHC::I16);
223 jump %ENTRY_CODE(Sp(0));
228 out = BaseReg + OFFSET_StgRegTable_rmp_tmp_w;
230 (retcode) = foreign "C" throwTo(MyCapability() "ptr",
236 switch [THROWTO_SUCCESS .. THROWTO_BLOCKED] (retcode) {
238 case THROWTO_SUCCESS: {
239 jump %ENTRY_CODE(Sp(0));
242 case THROWTO_BLOCKED: {
244 // we must block, and call throwToReleaseTarget() before returning
245 jump stg_block_throwto;
251 /* -----------------------------------------------------------------------------
253 -------------------------------------------------------------------------- */
261 /* Catch frames are very similar to update frames, but when entering
262 * one we just pop the frame off the stack and perform the correct
263 * kind of return to the activation record underneath us on the stack.
266 INFO_TABLE_RET(stg_catch_frame, CATCH_FRAME,
267 #if defined(PROFILING)
268 W_ unused1, W_ unused2,
270 W_ unused3, "ptr" W_ unused4)
273 Sp = Sp + SIZEOF_StgCatchFrame;
274 jump %ENTRY_CODE(Sp(SP_OFF));
280 Sp = Sp + SIZEOF_StgCatchFrame;
282 jump %ENTRY_CODE(Sp(SP_OFF));
286 /* -----------------------------------------------------------------------------
287 * The catch infotable
289 * This should be exactly the same as would be generated by this STG code
291 * catch = {x,h} \n {} -> catch#{x,h}
293 * It is used in deleteThread when reverting blackholes.
294 * -------------------------------------------------------------------------- */
296 INFO_TABLE(stg_catch,2,0,FUN,"catch","catch")
298 R2 = StgClosure_payload(R1,1); /* h */
299 R1 = StgClosure_payload(R1,0); /* x */
305 /* args: R1 = m :: IO a, R2 = handler :: Exception -> IO a */
306 STK_CHK_GEN(SIZEOF_StgCatchFrame + WDS(1), R1_PTR & R2_PTR, catchzh_fast);
308 /* Set up the catch frame */
309 Sp = Sp - SIZEOF_StgCatchFrame;
310 SET_HDR(Sp,stg_catch_frame_info,W_[CCCS]);
312 StgCatchFrame_handler(Sp) = R2;
313 StgCatchFrame_exceptions_blocked(Sp) = TO_W_(StgTSO_flags(CurrentTSO)) & TSO_BLOCKEX;
314 TICK_CATCHF_PUSHED();
316 /* Apply R1 to the realworld token */
322 /* -----------------------------------------------------------------------------
323 * The raise infotable
325 * This should be exactly the same as would be generated by this STG code
327 * raise = {err} \n {} -> raise#{err}
329 * It is used in raisezh_fast to update thunks on the update list
330 * -------------------------------------------------------------------------- */
332 INFO_TABLE(stg_raise,1,0,THUNK_1_0,"raise","raise")
334 R1 = StgThunk_payload(R1,0);
339 no_break_on_exception: W_[1];
342 INFO_TABLE_RET(stg_raise_ret, RET_SMALL, "ptr" W_ arg1)
346 W_[no_break_on_exception] = 1;
355 /* args : R1 :: Exception */
359 #if defined(PROFILING)
360 /* Debugging tool: on raising an exception, show where we are. */
362 /* ToDo: currently this is a hack. Would be much better if
363 * the info was only displayed for an *uncaught* exception.
365 if (RtsFlags_ProfFlags_showCCSOnException(RtsFlags) != 0::I32) {
366 foreign "C" fprintCCS_stderr(W_[CCCS] "ptr") [];
371 StgTSO_sp(CurrentTSO) = Sp;
372 (frame_type) = foreign "C" raiseExceptionHelper(BaseReg "ptr", CurrentTSO "ptr", exception "ptr") [];
373 Sp = StgTSO_sp(CurrentTSO);
374 if (frame_type == ATOMICALLY_FRAME) {
375 /* The exception has reached the edge of a memory transaction. Check that
376 * the transaction is valid. If not then perhaps the exception should
377 * not have been thrown: re-run the transaction. "trec" will either be
378 * a top-level transaction running the atomic block, or a nested
379 * transaction running an invariant check. In the latter case we
380 * abort and de-allocate the top-level transaction that encloses it
381 * as well (we could just abandon its transaction record, but this makes
382 * sure it's marked as aborted and available for re-use). */
385 trec = StgTSO_trec(CurrentTSO);
386 (r) = foreign "C" stmValidateNestOfTransactions(trec "ptr") [];
387 ("ptr" outer) = foreign "C" stmGetEnclosingTRec(trec "ptr") [];
388 foreign "C" stmAbortTransaction(MyCapability() "ptr", trec "ptr") [];
389 foreign "C" stmFreeAbortedTRec(MyCapability() "ptr", trec "ptr") [];
391 if (outer != NO_TREC) {
392 foreign "C" stmAbortTransaction(MyCapability() "ptr", outer "ptr") [];
393 foreign "C" stmFreeAbortedTRec(MyCapability() "ptr", outer "ptr") [];
396 StgTSO_trec(CurrentTSO) = NO_TREC;
398 // Transaction was valid: continue searching for a catch frame
399 Sp = Sp + SIZEOF_StgAtomicallyFrame;
400 goto retry_pop_stack;
402 // Transaction was not valid: we retry the exception (otherwise continue
403 // with a further call to raiseExceptionHelper)
404 ("ptr" trec) = foreign "C" stmStartTransaction(MyCapability() "ptr", NO_TREC "ptr") [];
405 StgTSO_trec(CurrentTSO) = trec;
406 R1 = StgAtomicallyFrame_code(Sp);
411 // After stripping the stack, see whether we should break here for
412 // GHCi (c.f. the -fbreak-on-exception flag). We do this after
413 // stripping the stack for a reason: we'll be inspecting values in
414 // GHCi, and it helps if all the thunks under evaluation have
415 // already been updated with the exception, rather than being left
417 if (W_[no_break_on_exception] != 0) {
418 W_[no_break_on_exception] = 0;
420 if (TO_W_(CInt[rts_stop_on_exception]) != 0) {
422 // we don't want any further exceptions to be caught,
423 // until GHCi is ready to handle them. This prevents
424 // deadlock if an exception is raised in InteractiveUI,
425 // for exmplae. Perhaps the stop_on_exception flag should
427 W_[rts_stop_on_exception] = 0;
428 ("ptr" ioAction) = foreign "C" deRefStablePtr (W_[rts_breakpoint_io_action] "ptr") [];
431 Sp(4) = stg_raise_ret_info;
432 Sp(3) = exception; // the AP_STACK
433 Sp(2) = base_GHCziBase_True_closure; // dummy breakpoint info
434 Sp(1) = base_GHCziBase_True_closure; // True <=> a breakpoint
436 jump stg_ap_pppv_info;
440 if (frame_type == STOP_FRAME) {
442 * We've stripped the entire stack, the thread is now dead.
443 * We will leave the stack in a GC'able state, see the stg_stop_thread
444 * entry code in StgStartup.cmm.
446 Sp = CurrentTSO + TSO_OFFSET_StgTSO_stack
447 + WDS(TO_W_(StgTSO_stack_size(CurrentTSO))) - WDS(2);
448 Sp(1) = exception; /* save the exception */
449 Sp(0) = stg_enter_info; /* so that GC can traverse this stack */
450 StgTSO_what_next(CurrentTSO) = ThreadKilled::I16;
451 SAVE_THREAD_STATE(); /* inline! */
453 jump stg_threadFinished;
456 /* Ok, Sp points to the enclosing CATCH_FRAME or CATCH_STM_FRAME. Pop everything
457 * down to and including this frame, update Su, push R1, and enter the handler.
459 if (frame_type == CATCH_FRAME) {
460 handler = StgCatchFrame_handler(Sp);
462 handler = StgCatchSTMFrame_handler(Sp);
465 /* Restore the blocked/unblocked state for asynchronous exceptions
466 * at the CATCH_FRAME.
468 * If exceptions were unblocked, arrange that they are unblocked
469 * again after executing the handler by pushing an
470 * unblockAsyncExceptions_ret stack frame.
472 * If we've reached an STM catch frame then roll back the nested
473 * transaction we were using.
477 if (frame_type == CATCH_FRAME) {
478 Sp = Sp + SIZEOF_StgCatchFrame;
479 if (StgCatchFrame_exceptions_blocked(frame) == 0) {
481 Sp(0) = stg_unblockAsyncExceptionszh_ret_info;
485 trec = StgTSO_trec(CurrentTSO);
486 ("ptr" outer) = foreign "C" stmGetEnclosingTRec(trec "ptr") [];
487 foreign "C" stmAbortTransaction(MyCapability() "ptr", trec "ptr") [];
488 foreign "C" stmFreeAbortedTRec(MyCapability() "ptr", trec "ptr") [];
489 StgTSO_trec(CurrentTSO) = outer;
490 Sp = Sp + SIZEOF_StgCatchSTMFrame;
493 /* Ensure that async excpetions are blocked when running the handler.
495 StgTSO_flags(CurrentTSO) =
496 StgTSO_flags(CurrentTSO) | TSO_BLOCKEX::I32 | TSO_INTERRUPTIBLE::I32;
498 /* Call the handler, passing the exception value and a realworld
499 * token as arguments.
507 jump RET_LBL(stg_ap_pv);
512 /* Args :: R1 :: Exception */