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 * ---------------------------------------------------------------------------*/
15 /* -----------------------------------------------------------------------------
18 A thread can request that asynchronous exceptions not be delivered
19 ("blocked") for the duration of an I/O computation. The primitive
21 blockAsyncExceptions# :: IO a -> IO a
23 is used for this purpose. During a blocked section, asynchronous
24 exceptions may be unblocked again temporarily:
26 unblockAsyncExceptions# :: IO a -> IO a
28 Furthermore, asynchronous exceptions are blocked automatically during
29 the execution of an exception handler. Both of these primitives
30 leave a continuation on the stack which reverts to the previous
31 state (blocked or unblocked) on exit.
33 A thread which wants to raise an exception in another thread (using
34 killThread#) must block until the target thread is ready to receive
35 it. The action of unblocking exceptions in a thread will release all
36 the threads waiting to deliver exceptions to that thread.
38 NB. there's a bug in here. If a thread is inside an
39 unsafePerformIO, and inside blockAsyncExceptions# (there is an
40 unblockAsyncExceptions_ret on the stack), and it is blocked in an
41 interruptible operation, and it receives an exception, then the
42 unsafePerformIO thunk will be updated with a stack object
43 containing the unblockAsyncExceptions_ret frame. Later, when
44 someone else evaluates this thunk, the blocked exception state is
45 not restored, and the result is that unblockAsyncExceptions_ret
46 will attempt to unblock exceptions in the current thread, but it'll
47 find that the CurrentTSO->blocked_exceptions is NULL. Hence, we
48 work around this by checking for NULL in awakenBlockedQueue().
50 -------------------------------------------------------------------------- */
52 INFO_TABLE_RET( stg_unblockAsyncExceptionszh_ret,
53 0/*framesize*/, 0/*bitmap*/, RET_SMALL )
55 // Not true: see comments above
56 // ASSERT(StgTSO_blocked_exceptions(CurrentTSO) != NULL);
57 #if defined(GRAN) || defined(PAR)
58 foreign "C" awakenBlockedQueue(StgTSO_blocked_exceptions(CurrentTSO) "ptr",
61 foreign "C" awakenBlockedQueue(StgTSO_blocked_exceptions(CurrentTSO) "ptr");
63 StgTSO_blocked_exceptions(CurrentTSO) = NULL;
66 jump %ENTRY_CODE(Sp(0));
70 jump %ENTRY_CODE(Sp(1));
74 INFO_TABLE_RET( stg_blockAsyncExceptionszh_ret,
75 0/*framesize*/, 0/*bitmap*/, RET_SMALL )
77 // Not true: see comments above
78 // ASSERT(StgTSO_blocked_exceptions(CurrentTSO) == NULL);
79 StgTSO_blocked_exceptions(CurrentTSO) = END_TSO_QUEUE;
82 jump %ENTRY_CODE(Sp(0));
86 jump %ENTRY_CODE(Sp(1));
90 blockAsyncExceptionszh_fast
92 /* Args: R1 :: IO a */
93 STK_CHK_GEN( WDS(2)/* worst case */, R1_PTR, blockAsyncExceptionszh_fast);
95 if (StgTSO_blocked_exceptions(CurrentTSO) == NULL) {
96 StgTSO_blocked_exceptions(CurrentTSO) = END_TSO_QUEUE;
97 /* avoid growing the stack unnecessarily */
98 if (Sp(0) == stg_blockAsyncExceptionszh_ret_info) {
102 Sp(0) = stg_unblockAsyncExceptionszh_ret_info;
108 jump RET_LBL(stg_ap_v);
111 unblockAsyncExceptionszh_fast
113 /* Args: R1 :: IO a */
114 STK_CHK_GEN( WDS(2), R1_PTR, unblockAsyncExceptionszh_fast);
116 if (StgTSO_blocked_exceptions(CurrentTSO) != NULL) {
117 #if defined(GRAN) || defined(PAR)
118 foreign "C" awakenBlockedQueue(StgTSO_blocked_exceptions(CurrentTSO) "ptr",
119 StgTSO_block_info(CurrentTSO) "ptr");
121 foreign "C" awakenBlockedQueue(StgTSO_blocked_exceptions(CurrentTSO) "ptr");
123 StgTSO_blocked_exceptions(CurrentTSO) = NULL;
125 /* avoid growing the stack unnecessarily */
126 if (Sp(0) == stg_unblockAsyncExceptionszh_ret_info) {
130 Sp(0) = stg_blockAsyncExceptionszh_ret_info;
136 jump RET_LBL(stg_ap_v);
140 #define interruptible(what_next) \
141 ( what_next == BlockedOnMVar \
142 || what_next == BlockedOnException \
143 || what_next == BlockedOnRead \
144 || what_next == BlockedOnWrite \
145 || what_next == BlockedOnDelay \
146 || what_next == BlockedOnDoProc)
150 /* args: R1 = TSO to kill, R2 = Exception */
154 /* This thread may have been relocated.
155 * (see Schedule.c:threadStackOverflow)
158 if (StgTSO_what_next(R1) == ThreadRelocated::I16) {
159 R1 = StgTSO_link(R1);
163 /* Determine whether this thread is interruptible or not */
165 /* If the target thread is currently blocking async exceptions,
166 * we'll have to block until it's ready to accept them. The
167 * exception is interruptible threads - ie. those that are blocked
170 why_blocked = TO_W_(StgTSO_why_blocked(R1));
171 if (StgTSO_blocked_exceptions(R1) != NULL && !interruptible(why_blocked))
173 StgTSO_link(CurrentTSO) = StgTSO_blocked_exceptions(R1);
174 StgTSO_blocked_exceptions(R1) = CurrentTSO;
176 StgTSO_why_blocked(CurrentTSO) = BlockedOnException::I16;
177 StgTSO_block_info(CurrentTSO) = R1;
179 BLOCK( R1_PTR & R2_PTR, killThreadzh_fast );
182 /* Killed threads turn into zombies, which might be garbage
183 * collected at a later date. That's why we don't have to
184 * explicitly remove them from any queues they might be on.
187 /* We might have killed ourselves. In which case, better be *very*
188 * careful. If the exception killed us, then return to the scheduler.
189 * If the exception went to a catch frame, we'll just continue from
192 if (R1 == CurrentTSO) {
194 foreign "C" raiseAsyncWithLock(R1 "ptr", R2 "ptr");
195 if (StgTSO_what_next(CurrentTSO) == ThreadKilled::I16) {
200 ASSERT(StgTSO_what_next(CurrentTSO) == ThreadRunGHC::I16);
201 jump %ENTRY_CODE(Sp(0));
204 foreign "C" raiseAsyncWithLock(R1 "ptr", R2 "ptr");
207 jump %ENTRY_CODE(Sp(0));
210 /* -----------------------------------------------------------------------------
212 -------------------------------------------------------------------------- */
215 #define CATCH_FRAME_ENTRY_TEMPLATE(label,ret) \
218 Sp = Sp + SIZEOF_StgCatchFrame; \
222 #define CATCH_FRAME_ENTRY_TEMPLATE(label,ret) \
227 Sp = Sp + SIZEOF_StgCatchFrame; \
239 CATCH_FRAME_ENTRY_TEMPLATE(stg_catch_frame_0_ret,%RET_VEC(Sp(SP_OFF),0))
240 CATCH_FRAME_ENTRY_TEMPLATE(stg_catch_frame_1_ret,%RET_VEC(Sp(SP_OFF),1))
241 CATCH_FRAME_ENTRY_TEMPLATE(stg_catch_frame_2_ret,%RET_VEC(Sp(SP_OFF),2))
242 CATCH_FRAME_ENTRY_TEMPLATE(stg_catch_frame_3_ret,%RET_VEC(Sp(SP_OFF),3))
243 CATCH_FRAME_ENTRY_TEMPLATE(stg_catch_frame_4_ret,%RET_VEC(Sp(SP_OFF),4))
244 CATCH_FRAME_ENTRY_TEMPLATE(stg_catch_frame_5_ret,%RET_VEC(Sp(SP_OFF),5))
245 CATCH_FRAME_ENTRY_TEMPLATE(stg_catch_frame_6_ret,%RET_VEC(Sp(SP_OFF),6))
246 CATCH_FRAME_ENTRY_TEMPLATE(stg_catch_frame_7_ret,%RET_VEC(Sp(SP_OFF),7))
248 #if MAX_VECTORED_RTN > 8
249 #error MAX_VECTORED_RTN has changed: please modify stg_catch_frame too.
252 #if defined(PROFILING)
253 #define CATCH_FRAME_BITMAP 7
254 #define CATCH_FRAME_WORDS 4
256 #define CATCH_FRAME_BITMAP 1
257 #define CATCH_FRAME_WORDS 2
260 /* Catch frames are very similar to update frames, but when entering
261 * one we just pop the frame off the stack and perform the correct
262 * kind of return to the activation record underneath us on the stack.
265 INFO_TABLE_RET(stg_catch_frame,
266 CATCH_FRAME_WORDS, CATCH_FRAME_BITMAP,
268 stg_catch_frame_0_ret,
269 stg_catch_frame_1_ret,
270 stg_catch_frame_2_ret,
271 stg_catch_frame_3_ret,
272 stg_catch_frame_4_ret,
273 stg_catch_frame_5_ret,
274 stg_catch_frame_6_ret,
275 stg_catch_frame_7_ret)
276 CATCH_FRAME_ENTRY_TEMPLATE(,%ENTRY_CODE(Sp(SP_OFF)))
278 /* -----------------------------------------------------------------------------
279 * The catch infotable
281 * This should be exactly the same as would be generated by this STG code
283 * catch = {x,h} \n {} -> catch#{x,h}
285 * It is used in deleteThread when reverting blackholes.
286 * -------------------------------------------------------------------------- */
288 INFO_TABLE(stg_catch,2,0,FUN,"catch","catch")
290 R2 = StgClosure_payload(R1,1); /* h */
291 R1 = StgClosure_payload(R1,0); /* x */
297 /* args: R1 = m :: IO a, R2 = handler :: Exception -> IO a */
298 STK_CHK_GEN(SIZEOF_StgCatchFrame + WDS(1), R1_PTR & R2_PTR, catchzh_fast);
300 /* Set up the catch frame */
301 Sp = Sp - SIZEOF_StgCatchFrame;
302 SET_HDR(Sp,stg_catch_frame_info,W_[CCCS]);
304 StgCatchFrame_handler(Sp) = R2;
305 StgCatchFrame_exceptions_blocked(Sp) =
306 (StgTSO_blocked_exceptions(CurrentTSO) != NULL);
307 TICK_CATCHF_PUSHED();
309 /* Apply R1 to the realworld token */
313 jump RET_LBL(stg_ap_v);
316 /* -----------------------------------------------------------------------------
317 * The raise infotable
319 * This should be exactly the same as would be generated by this STG code
321 * raise = {err} \n {} -> raise#{err}
323 * It is used in raisezh_fast to update thunks on the update list
324 * -------------------------------------------------------------------------- */
326 INFO_TABLE(stg_raise,1,0,THUNK,"raise","raise")
328 R1 = StgThunk_payload(R1,0);
337 /* args : R1 :: Exception */
340 #if defined(PROFILING)
341 /* Debugging tool: on raising an exception, show where we are. */
343 /* ToDo: currently this is a hack. Would be much better if
344 * the info was only displayed for an *uncaught* exception.
346 if (RtsFlags_ProfFlags_showCCSOnException(RtsFlags)) {
347 foreign "C" fprintCCS_stderr(W_[CCCS] "ptr");
352 StgTSO_sp(CurrentTSO) = Sp;
353 frame_type = foreign "C" raiseExceptionHelper(CurrentTSO "ptr", R1 "ptr");
354 Sp = StgTSO_sp(CurrentTSO);
355 if (frame_type == ATOMICALLY_FRAME) {
356 /* The exception has reached the edge of a memory transaction. Check that
357 * the transaction is valid. If not then perhaps the exception should
358 * not have been thrown: re-run the transaction */
361 trec = StgTSO_trec(CurrentTSO);
362 r = foreign "C" stmValidateNestOfTransactions(trec "ptr");
363 foreign "C" stmAbortTransaction(trec "ptr");
364 StgTSO_trec(CurrentTSO) = NO_TREC;
366 // Transaction was valid: continue searching for a catch frame
367 Sp = Sp + SIZEOF_StgAtomicallyFrame;
368 goto retry_pop_stack;
370 // Transaction was not valid: we retry the exception (otherwise continue
371 // with a further call to raiseExceptionHelper)
372 "ptr" trec = foreign "C" stmStartTransaction(BaseReg "ptr", NO_TREC "ptr");
373 StgTSO_trec(CurrentTSO) = trec;
374 R1 = StgAtomicallyFrame_code(Sp);
376 jump RET_LBL(stg_ap_v);
380 if (frame_type == STOP_FRAME) {
381 /* We've stripped the entire stack, the thread is now dead. */
382 Sp = CurrentTSO + OFFSET_StgTSO_stack
383 + WDS(StgTSO_stack_size(CurrentTSO)) - WDS(1);
384 Sp(0) = R1; /* save the exception */
385 StgTSO_what_next(CurrentTSO) = ThreadKilled::I16;
386 SAVE_THREAD_STATE(); /* inline! */
391 /* Ok, Sp points to the enclosing CATCH_FRAME or CATCH_STM_FRAME. Pop everything
392 * down to and including this frame, update Su, push R1, and enter the handler.
394 if (frame_type == CATCH_FRAME) {
395 handler = StgCatchFrame_handler(Sp);
397 handler = StgCatchSTMFrame_handler(Sp);
400 /* Restore the blocked/unblocked state for asynchronous exceptions
401 * at the CATCH_FRAME.
403 * If exceptions were unblocked, arrange that they are unblocked
404 * again after executing the handler by pushing an
405 * unblockAsyncExceptions_ret stack frame.
409 if (frame_type == CATCH_FRAME) {
410 Sp = Sp + SIZEOF_StgCatchFrame;
411 if (StgCatchFrame_exceptions_blocked(frame) == 0) {
413 Sp(0) = stg_unblockAsyncExceptionszh_ret_info;
416 Sp = Sp + SIZEOF_StgCatchSTMFrame;
419 /* Ensure that async excpetions are blocked when running the handler.
421 if (StgTSO_blocked_exceptions(CurrentTSO) == NULL) {
422 StgTSO_blocked_exceptions(CurrentTSO) = END_TSO_QUEUE;
425 /* Call the handler, passing the exception value and a realworld
426 * token as arguments.
434 jump RET_LBL(stg_ap_pv);
439 /* Args :: R1 :: Exception */