1 .\" $NetBSD: ctxsw.9,v 1.2 1996/12/02 00:11:31 tls Exp $
3 .\" Copyright (c) 1996 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
5 .\" Copyright (c) 2023 The FreeBSD Foundation
7 .\" This code is derived from software contributed to The NetBSD Foundation
8 .\" by Paul Kranenburg.
10 .\" Portions of this documentation were written by Mitchell Horne
11 .\" under sponsorship from the FreeBSD Foundation.
13 .\" Redistribution and use in source and binary forms, with or without
14 .\" modification, are permitted provided that the following conditions
16 .\" 1. Redistributions of source code must retain the above copyright
17 .\" notice, this list of conditions and the following disclaimer.
18 .\" 2. Redistributions in binary form must reproduce the above copyright
19 .\" notice, this list of conditions and the following disclaimer in the
20 .\" documentation and/or other materials provided with the distribution.
22 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
26 .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 .\" POSSIBILITY OF SUCH DAMAGE.
43 .Nd switch to another thread context
48 .Fn mi_switch "int flags"
50 .Fn cpu_switch "struct thread *oldtd" "struct thread *newtd" "struct mtx *lock"
52 .Fn cpu_throw "struct thread *oldtd" "struct thread *newtd"
56 function implements the machine-independent prelude to a thread context
58 It is the single entry point for every context switch and is called from only
59 a few distinguished places in the kernel.
60 The context switch is, by necessity, always performed by the switched thread,
61 even when the switch is initiated from elsewhere; e.g. preemption requested via
62 Inter-Processor Interrupt (IPI).
64 The various major uses of
66 can be enumerated as follows:
67 .Bl -enum -offset indent
69 From within a function such as
73 when the current thread
74 voluntarily relinquishes the CPU to wait for some resource or lock to become
77 Involuntary preemption due to arrival of a higher-priority thread.
81 if preemption was deferred due to the critical section.
83 Within the TDA_SCHED AST handler, when rescheduling before the return to
84 usermode was requested.
85 There are several reasons for this, a notable one coming from
87 when the running thread has exceeded its time slice.
89 In the signal handling code
92 if a signal is delivered that causes a process to stop.
95 .Fn thread_suspend_check
96 where a thread needs to stop execution due to the suspension state of
97 the process as a whole.
101 when a thread wants to voluntarily relinquish the processor.
108 indicates the context switch type.
109 One of the following must be passed:
110 .Bl -tag -offset indent -width "SWT_REMOTEWAKEIDLE"
111 .It Dv SWT_OWEPREEMPT
112 Switch due to delayed preemption after exiting a critical section.
114 Switch after propagating scheduling priority to the owner of a resource.
118 .It Dv SWT_RELINQUISH
120 .It Dv SWT_NEEDRESCHED
121 Rescheduling was requested.
123 Switch from the idle thread.
125 A kernel thread which handles interrupts has finished work and must wait for
126 interrupts to schedule additional work.
129 .It Dv SWT_REMOTEPREEMPT
130 Preemption by a higher-priority thread, initiated by a remote processor.
131 .It Dv SWT_REMOTEWAKEIDLE
132 Idle thread preempted, initiated by a remote processor.
134 The running thread has been bound to another processor and must be switched
138 In addition to the switch type, callers must specify the nature of the
139 switch by performing a bitwise OR with one of the
144 Respectively, these flags denote whether the context switch is voluntary or
145 involuntary on the part of the current thread.
146 For an involuntary context switch in which the running thread is
147 being preempted, the caller should also pass the
153 the current thread must be holding its assigned thread lock.
154 It may be unlocked as part of the context switch.
155 After they have been rescheduled and execution resumes, threads will exit
157 with their thread lock unlocked.
160 records the amount of time the current thread has been running before handing
161 control over to the scheduler, via
163 After selecting a new thread to run, the scheduler will call
165 to perform the low-level context switch.
168 is the machine-dependent function that performs the actual switch from the
173 First, it saves the context of
175 to its Process Control Block,
182 The function then updates important per-CPU state such as the
184 variable, and activates
186 virtual address space using its associated
189 Finally, it reads in the saved context from
192 CPU instruction flow continues in the new thread context, on
197 can be understood as a completion of the function call initiated by
199 when it was previously switched out, at some point in the distant (relative to
206 is used to pass the mutex which will be stored as
208 thread lock at the moment that
210 is completely switched out.
211 This is an implementation detail of
217 except that it does not save the context of the old thread.
218 This function is useful when the kernel does not have an old thread
219 context to save, such as when CPUs other than the boot CPU perform their
220 first task switch, or when the kernel does not care about the state of the
221 old thread, such as in
223 when the kernel terminates the current thread and switches into a new
230 .Xr critical_exit 9 ,