xref: /xnu-10002.81.5/osfmk/man/thread_abort_safely.html (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions<h2>thread_abort_safely</h2>
2*5e3eaea3SApple OSS Distributions<hr>
3*5e3eaea3SApple OSS Distributions<p>
4*5e3eaea3SApple OSS Distributions<strong>Function</strong> - Abort a thread, restartably.
5*5e3eaea3SApple OSS Distributions<h3>SYNOPSIS</h3>
6*5e3eaea3SApple OSS Distributions<pre>
7*5e3eaea3SApple OSS Distributions<strong>kern_return_t   thread_abort_safely</strong>
8*5e3eaea3SApple OSS Distributions                <strong>(thread_act_t</strong>                     <var>target_thread</var><strong>);</strong>
9*5e3eaea3SApple OSS Distributions</pre>
10*5e3eaea3SApple OSS Distributions<h3>PARAMETERS</h3>
11*5e3eaea3SApple OSS Distributions<dl>
12*5e3eaea3SApple OSS Distributions<p>
13*5e3eaea3SApple OSS Distributions<dt> <var>target_thread</var>
14*5e3eaea3SApple OSS Distributions<dd>
15*5e3eaea3SApple OSS Distributions[in thread send right]
16*5e3eaea3SApple OSS DistributionsThe thread to be aborted.
17*5e3eaea3SApple OSS Distributions</dl>
18*5e3eaea3SApple OSS Distributions<h3>DESCRIPTION</h3>
19*5e3eaea3SApple OSS Distributions<p>
20*5e3eaea3SApple OSS DistributionsThe <strong>thread_abort_safely</strong> function aborts page faults and any message
21*5e3eaea3SApple OSS Distributionsprimitive calls in use by <var>target_thread</var>.  Scheduling depressions
22*5e3eaea3SApple OSS Distributionsand clock sleeps are
23*5e3eaea3SApple OSS Distributionsalso aborted.  The call returns a code indicating that it was
24*5e3eaea3SApple OSS Distributionsinterrupted.  The call
25*5e3eaea3SApple OSS Distributionsis interrupted even if the thread (or the task containing it)
26*5e3eaea3SApple OSS Distributionsis suspended.  If it is
27*5e3eaea3SApple OSS Distributionssuspended, the thread receives the interrupt when it resumes.
28*5e3eaea3SApple OSS Distributions<p>
29*5e3eaea3SApple OSS DistributionsIf its state is not modified before it resumes, the thread will
30*5e3eaea3SApple OSS Distributionsretry an aborted
31*5e3eaea3SApple OSS Distributionspage fault.  The Mach message trap returns either
32*5e3eaea3SApple OSS Distributions<strong>MACH_SEND_INTERRUPTED</strong> or <strong>MACH_RCV_INTERRUPTED</strong>, depending
33*5e3eaea3SApple OSS Distributionson whether the send or the
34*5e3eaea3SApple OSS Distributionsreceive side was interrupted.  Note, though, that the Mach message trap is
35*5e3eaea3SApple OSS Distributionscontained within the <strong>mach_msg</strong> library routine, which,
36*5e3eaea3SApple OSS Distributionsby default, retries
37*5e3eaea3SApple OSS Distributionsinterrupted message calls.
38*5e3eaea3SApple OSS Distributions<p>
39*5e3eaea3SApple OSS DistributionsThe basic purpose of <strong>thread_abort_safely</strong> is to let
40*5e3eaea3SApple OSS Distributionsone thread cleanly stop
41*5e3eaea3SApple OSS Distributionsanother thread (<var>target_thread</var>).  The target thread is stopped
42*5e3eaea3SApple OSS Distributionsin such a manner that
43*5e3eaea3SApple OSS Distributionsits future execution can be controlled in a predictable way.  When
44*5e3eaea3SApple OSS Distributions<strong>thread_abort_safely</strong> returns (if successful), the target
45*5e3eaea3SApple OSS Distributionsthread will appear to have just
46*5e3eaea3SApple OSS Distributionsreturned from the kernel (if it had been in kernel mode).
47*5e3eaea3SApple OSS Distributions<h3>NOTES</h3>
48*5e3eaea3SApple OSS Distributions<p>
49*5e3eaea3SApple OSS DistributionsBy way of comparison, the <strong>thread_suspend</strong> function keeps
50*5e3eaea3SApple OSS Distributionsthe target thread
51*5e3eaea3SApple OSS Distributionsfrom executing any further instructions at the user level, including
52*5e3eaea3SApple OSS Distributionsthe return
53*5e3eaea3SApple OSS Distributionsfrom a system call.  The <strong>thread_get_state</strong> function
54*5e3eaea3SApple OSS Distributionsreturns the thread's user
55*5e3eaea3SApple OSS Distributionsstate, while <strong>thread_set_state</strong> allows modification of the user state.
56*5e3eaea3SApple OSS Distributions<p>
57*5e3eaea3SApple OSS DistributionsA problem occurs if a suspended thread had been executing within a system
58*5e3eaea3SApple OSS Distributionscall.  In this case, the thread has, not only a user state, but
59*5e3eaea3SApple OSS Distributionsan associated kernel
60*5e3eaea3SApple OSS Distributionsstate. (The kernel state cannot be changed with <strong>thread_set_state</strong>.)
61*5e3eaea3SApple OSS DistributionsAs a result,
62*5e3eaea3SApple OSS Distributionswhen the thread resumes, the system call can return, producing a change in the
63*5e3eaea3SApple OSS Distributionsuser state and, possibly, user memory.
64*5e3eaea3SApple OSS Distributions<p>
65*5e3eaea3SApple OSS DistributionsFor a thread executing within a system call, <strong>thread_abort_safely</strong>
66*5e3eaea3SApple OSS Distributionsaborts the
67*5e3eaea3SApple OSS Distributionskernel call from the thread's point of view.  Specifically, it
68*5e3eaea3SApple OSS Distributionsresets the kernel state so
69*5e3eaea3SApple OSS Distributionsthat the thread will resume execution at the system call return,
70*5e3eaea3SApple OSS Distributionswith the return
71*5e3eaea3SApple OSS Distributionscode value set to one of the interrupted codes.  The system call itself may
72*5e3eaea3SApple OSS Distributionscompleted entirely, aborted entirely or be partially completed,
73*5e3eaea3SApple OSS Distributionsdepending on when
74*5e3eaea3SApple OSS Distributionsthe abort is received.  As a result, if the thread's user state
75*5e3eaea3SApple OSS Distributionshas been modified by
76*5e3eaea3SApple OSS Distributions<strong>thread_set_state</strong>, it will not be altered un-predictably
77*5e3eaea3SApple OSS Distributionsby any unexpected
78*5e3eaea3SApple OSS Distributionssystem call side effects.
79*5e3eaea3SApple OSS Distributions<p>
80*5e3eaea3SApple OSS DistributionsFor example, to simulate a POSIX signal, use the following sequence of calls:
81*5e3eaea3SApple OSS Distributions<dl>
82*5e3eaea3SApple OSS Distributions<dd>
83*5e3eaea3SApple OSS Distributions<strong>thread_suspend</strong>\(emTo stop the thread.
84*5e3eaea3SApple OSS Distributions<dd>
85*5e3eaea3SApple OSS Distributions<strong>thread_abort_safely</strong>\(emTo interrupt any system call in
86*5e3eaea3SApple OSS Distributionsprogress and set the
87*5e3eaea3SApple OSS Distributionsreturn value to "interrupted".  Because the thread is already stopped, it will
88*5e3eaea3SApple OSS Distributionsnot return to user code.
89*5e3eaea3SApple OSS Distributions<dd>
90*5e3eaea3SApple OSS Distributions<strong>thread_set_state</strong>\(emTo modify the thread's user state to simulate a
91*5e3eaea3SApple OSS Distributionsprocedure call to the signal handler.
92*5e3eaea3SApple OSS Distributions<dd>
93*5e3eaea3SApple OSS Distributions<strong>thread_resume</strong>\(emTo resume execution at the signal handler.
94*5e3eaea3SApple OSS DistributionsIf the thread's
95*5e3eaea3SApple OSS Distributionsstack is set up correctly, the thread can return to the interrupted
96*5e3eaea3SApple OSS Distributionssystem call.
97*5e3eaea3SApple OSS DistributionsNote that the code to push an extra stack frame and change the registers is
98*5e3eaea3SApple OSS Distributionshighly machine dependent.
99*5e3eaea3SApple OSS Distributions</dl>
100*5e3eaea3SApple OSS Distributions<h3>CAUTIONS</h3>
101*5e3eaea3SApple OSS Distributions<p>
102*5e3eaea3SApple OSS DistributionsAs a rule, do not use <strong>thread_abort_safely</strong> on a non-suspended
103*5e3eaea3SApple OSS Distributionsthread.  This
104*5e3eaea3SApple OSS Distributionsoperation is very risky because it is difficult to know which
105*5e3eaea3SApple OSS Distributionssystem trap, if any, is
106*5e3eaea3SApple OSS Distributionsexecuting and whether an interrupt return will result in some useful action by
107*5e3eaea3SApple OSS Distributionsthe thread.
108*5e3eaea3SApple OSS Distributions<p>
109*5e3eaea3SApple OSS Distributions<strong>thread_abort_safely</strong> will not abort any non-atomic operation
110*5e3eaea3SApple OSS Distributions(such as a
111*5e3eaea3SApple OSS Distributionsmulti-page <strong>memory_object_data_supply</strong> or exception processing)
112*5e3eaea3SApple OSS Distributionsbut will return an
113*5e3eaea3SApple OSS Distributionserror instead.  The caller of this function must then allow the
114*5e3eaea3SApple OSS Distributionsthread to resume
115*5e3eaea3SApple OSS Distributionsand attempt to abort it later.  If the thread must be aborted,
116*5e3eaea3SApple OSS Distributionseven if doing so
117*5e3eaea3SApple OSS Distributionswould abort any non-atomic operations, <strong>thread_abort</strong> would be used.
118*5e3eaea3SApple OSS Distributions<h3>RETURN VALUES</h3>
119*5e3eaea3SApple OSS Distributions<dl>
120*5e3eaea3SApple OSS Distributions<p>
121*5e3eaea3SApple OSS Distributions<dt> <strong>KERN_FAILURE</strong>
122*5e3eaea3SApple OSS Distributions<dd>
123*5e3eaea3SApple OSS DistributionsThe thread is in the middle of a non-restartable operation.
124*5e3eaea3SApple OSS Distributions</dl>
125*5e3eaea3SApple OSS Distributions<h3>RELATED INFORMATION</h3>
126*5e3eaea3SApple OSS Distributions<p>
127*5e3eaea3SApple OSS DistributionsFunctions:
128*5e3eaea3SApple OSS Distributions<a href="mach_msg.html"><strong>mach_msg</strong></a>,
129*5e3eaea3SApple OSS Distributions<a href="thread_get_state.html"><strong>thread_get_state</strong></a>,
130*5e3eaea3SApple OSS Distributions<a href="thread_info.html"><strong>thread_info</strong></a>,
131*5e3eaea3SApple OSS Distributions<a href="thread_set_state.html"><strong>thread_set_state</strong></a>,
132*5e3eaea3SApple OSS Distributions<a href="thread_suspend.html"><strong>thread_suspend</strong></a>,
133*5e3eaea3SApple OSS Distributions<a href="thread_terminate.html"><strong>thread_terminate</strong></a>,
134*5e3eaea3SApple OSS Distributions<a href="thread_abort.html"><strong>thread_abort</strong></a>.
135