xref: /freebsd/share/man/man9/mi_switch.9 (revision 952d112864d8008aa87278a30a539d888a8493cd)
1.\"	$NetBSD: ctxsw.9,v 1.2 1996/12/02 00:11:31 tls Exp $
2.\"
3.\" Copyright (c) 1996 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Paul Kranenburg.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\" 3. All advertising materials mentioning features or use of this software
18.\"    must display the following acknowledgement:
19.\"        This product includes software developed by the NetBSD
20.\"        Foundation, Inc. and its contributors.
21.\" 4. Neither the name of The NetBSD Foundation nor the names of its
22.\"    contributors may be used to endorse or promote products derived
23.\"    from this software without specific prior written permission.
24.\"
25.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
29.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35.\" POSSIBILITY OF SUCH DAMAGE.
36.\"
37.Dd Nov 24, 1996
38.Dt MI_SWITCH 9
39.Os FreeBSD
40.Sh NAME
41.Nm mi_switch ,
42.Nm cpu_switch
43.Nd switch to another process context
44.Sh SYNOPSIS
45.Ft void
46.Fn mi_switch "void"
47.Ft void
48.Fn cpu_switch "void"
49.Sh DESCRIPTION
50The
51.Fn mi_switch
52function implements the machine independent prelude to a process context
53switch. It is called from only a few distinguished places in the kernel
54code as a result of the principle of non-preemtable kernel mode execution.
55The three major uses of
56.Nm
57can be enumerated as follows:
58.Bl -enum -offset indent
59.It
60from within
61.Xr sleep 9 , and
62.Xr tsleep 9
63when the current process
64voluntarily relinquishes the CPU to wait for some resource to become
65available.
66.It
67after handling a trap
68.Pq e.g. a system call, device interrupt
69when the kernel prepares a return to user-mode execution. This case is
70typically handled by machine dependent trap-handling code after detection
71of a change in the signal disposition of the current process, or when a
72higher priority process might be available to run. The latter event is
73communicated by the machine independent scheduling routines by calling
74the machine defined
75.Fn need_resched "void" .
76.It
77in the signal handling code
78.Pq see Xr issignal 9
79if a signal is delivered that causes a process to stop.
80.El
81.Pp
82.Fn mi_switch
83records the amount of time the current process has been running in the
84process structure and checks this value against the CPU time limits
85allocated to the process
86.Pq see Xr getrlimit 2 .
87Exceeding the soft limit results in a
88.Dv SIGXCPU
89signal to be posted to the process, while exceeding the hard limit will
90cause a
91.Dv SIGKILL .
92After these administrative tasks are done,
93.Fn mi_switch
94hands over control to the machine dependent routine
95.Fn cpu_switch "void" ,
96which will perform the actual process context switch.
97.Pp
98.Fn cpu_switch
99will make a choice amongst the processes which are ready to run from a
100priority queue data-structure. The priority queue consists of an array
101.Va qs[NQS]
102of queue header structures each of which identifies a list of runnable
103processes of equal priority
104.Pq see Fa <sys/proc.h> .
105A single word
106.Va whichqs
107containing a bit mask identifying non-empty queues assists in selecting
108a process quickly.
109.Fn cpu_switch
110must remove the first process from the list on the queue
111with the highest priority
112.Po lower indices in Va sq
113indicate higher priority
114.Pc ,
115and assign the address of its process structure to the global variable
116.Dv curproc .
117If no processes are available on the run queues,
118.Fn cpu_switch
119shall go into an
120.Dq idle
121loop. The idle loop must allow interrupts to be taken that will eventually
122cause processes to appear again on the run queues. The variable
123.Va curproc
124should be
125.Dv NULL
126while
127.Fn cpu_switch
128waits for this to happen.
129.Pp
130Note that
131.Fn mi_switch
132and thus
133.Fn cpu_switch
134should be called at splhigh().
135.Pp
136.Sh SEE ALSO
137.Xr issignal 9 ,
138.Xr spl 9 ,
139.Xr tsleep 9 ,
140.Xr wakeup 9
141.Pp
142