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