xref: /freebsd/share/man/man9/mi_switch.9 (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
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.\" $FreeBSD$
38.\"
39.Dd Nov 24, 1996
40.Dt MI_SWITCH 9
41.Os FreeBSD
42.Sh NAME
43.Nm mi_switch ,
44.Nm cpu_switch
45.Nd switch to another process context
46.Sh SYNOPSIS
47.Fd #include <sys/param.h>
48.Fd #include <sys/proc.h>
49.Ft void
50.Fn mi_switch "void"
51.Ft void
52.Fn cpu_switch "struct proc *p"
53.Sh DESCRIPTION
54The
55.Fn mi_switch
56function implements the machine independent prelude to a process context
57switch.
58It is called from only a few distinguished places in the kernel
59code as a result of the principle of non-preemtable kernel mode execution.
60The three major uses of
61.Nm
62can be enumerated as follows:
63.Bl -enum -offset indent
64.It
65from within
66.Xr sleep 9 , and
67.Xr tsleep 9
68when the current process
69voluntarily relinquishes the CPU to wait for some resource to become
70available.
71.It
72after handling a trap
73.Pq e.g. a system call, device interrupt
74when the kernel prepares a return to user-mode execution.
75This case is
76typically handled by machine dependent trap-handling code after detection
77of a change in the signal disposition of the current process, or when a
78higher priority process might be available to run.
79The latter event is
80communicated by the machine independent scheduling routines by calling
81the machine defined
82.Fn need_resched .
83.It
84in the signal handling code
85.Pq see Xr issignal 9
86if a signal is delivered that causes a process to stop.
87.El
88.Pp
89.Fn mi_switch
90records the amount of time the current process has been running in the
91process structure and checks this value against the CPU time limits
92allocated to the process
93.Pq see Xr getrlimit 2 .
94Exceeding the soft limit results in a
95.Dv SIGXCPU
96signal to be posted to the process, while exceeding the hard limit will
97cause a
98.Dv SIGKILL .
99After these administrative tasks are done,
100.Fn mi_switch
101hands over control to the machine dependent routine
102.Fn cpu_switch ,
103which will perform the actual process context switch.
104.Pp
105.Fn cpu_switch
106will make a choice amongst the processes which are ready to run from a
107priority queue data-structure.
108The priority queue consists of an array
109.Va qs[NQS]
110of queue header structures each of which identifies a list of runnable
111processes of equal priority
112.Pq see Fa <sys/proc.h> .
113A single word
114.Va whichqs
115containing a bit mask identifying non-empty queues assists in selecting
116a process quickly.
117.Fn cpu_switch
118must remove the first process from the list on the queue
119with the highest priority
120.Po lower indices in Va qs
121indicate higher priority
122.Pc ,
123and assign the address of its process structure to the global variable
124.Dv curproc .
125If no processes are available on the run queues,
126.Fn cpu_switch
127shall go into an
128.Dq idle
129loop.
130The idle loop must allow interrupts to be taken that will eventually
131cause processes to appear again on the run queues.
132The variable
133.Va curproc
134should be
135.Dv NULL
136while
137.Fn cpu_switch
138waits for this to happen.
139.Pp
140Note that
141.Fn mi_switch
142and thus
143.Fn cpu_switch
144should be called at splhigh().
145.Pp
146.Sh SEE ALSO
147.Xr issignal 9 ,
148.Xr spl 9 ,
149.Xr tsleep 9 ,
150.Xr wakeup 9
151.Pp
152