1.\" $NetBSD: mi_switch.9,v 1.2 2008/04/30 13:10:58 martin Exp $ 2.\" 3.\" Copyright (c) 1996, 2002 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.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.\" POSSIBILITY OF SUCH DAMAGE. 29.\" 30.Dd July 21, 2007 31.Dt MI_SWITCH 9 32.Os 33.Sh NAME 34.Nm mi_switch 35.Nd machine independent context switch prelude 36.Sh SYNOPSIS 37.Ft int 38.Fn mi_switch "struct lwp *l" 39.Sh DESCRIPTION 40The 41.Fn mi_switch 42function implements the machine-independent prelude to an LWP context 43switch. It is called from only a few distinguished places in the 44kernel code as a result of the principle of non-preemptable kernel 45mode execution. The three major uses of 46.Fn mi_switch 47can be enumerated as follows: 48.Bl -enum -offset indent 49.It 50From within 51.Xr cv_wait 9 52and associated methods 53when the current LWP voluntarily relinquishes the CPU to wait for 54some resource to become available. 55.It 56From within 57.Xr preempt 9 58when the current LWP voluntarily relinquishes the CPU or when the 59kernel prepares a return to user-mode execution. 60.It 61In the signal handling code 62if a signal is delivered that causes an LWP to stop 63.Pq see Xr issignal 9 . 64.El 65.Pp 66.Fn mi_switch 67records the amount of time the current LWP has been running in the 68LWP structure and checks this value against the CPU time limits 69allocated to the LWP 70.Pq see Xr getrlimit 2 . 71Exceeding the soft limit results in a 72.Dv SIGXCPU 73signal to be posted to the LWP, while exceeding the hard limit will 74cause a 75.Dv SIGKILL . 76.Pp 77Unless 78.Fa l->l_switchto 79is not 80.Dv NULL , 81.Fn mi_switch 82will call 83.Fn sched_nextlwp 84to select a new LWP from the scheduler's runqueue structures. If no 85runnable LWP is found, the idle LWP is used. 86If the new LWP is not equal to the current one, 87.Fn mi_switch 88will hand over control to the machine-dependent function 89.Xr cpu_switchto 9 90to switch to the new LWP. 91.Pp 92.Fn mi_switch 93has to be called with the LWP lock held 94(through calling 95.Fn lwp_lock 96first) and at the 97.Xr splsched 9 98interrupt protection level. It returns with the LWP lock released. 99.Sh RETURN VALUES 100.Fn mi_switch 101returns 1 if a context switch was performed to a 102different LWP, 0 otherwise. 103.Sh SEE ALSO 104.Xr condvar 9 , 105.Xr cpu_switchto 9 , 106.Xr csf 9 , 107.Xr pmap 9 , 108.Xr ras 9 , 109.Xr sched_4bsd 9 , 110.Xr splsched 9 111