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