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