1.\" $NetBSD: mi_switch.9,v 1.4 2017/07/03 21:28:48 wiz 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. 44It is called from only a few distinguished places in the kernel 45code as a result of the principle of non-preemptable kernel mode 46execution. 47The three major uses of 48.Fn mi_switch 49can be enumerated as follows: 50.Bl -enum -offset indent 51.It 52From within 53.Xr cv_wait 9 54and associated methods 55when the current LWP voluntarily relinquishes the CPU to wait for 56some resource to become available. 57.It 58From within 59.Xr preempt 9 60when the current LWP voluntarily relinquishes the CPU or when the 61kernel prepares a return to user-mode execution. 62.It 63In the signal handling code 64if a signal is delivered that causes an LWP to stop 65.Pq see Xr issignal 9 . 66.El 67.Pp 68.Fn mi_switch 69records the amount of time the current LWP has been running in the 70LWP structure and checks this value against the CPU time limits 71allocated to the LWP 72.Pq see Xr getrlimit 2 . 73Exceeding the soft limit results in a 74.Dv SIGXCPU 75signal to be posted to the LWP, while exceeding the hard limit will 76cause a 77.Dv SIGKILL . 78.Pp 79Unless 80.Fa l->l_switchto 81is not 82.Dv NULL , 83.Fn mi_switch 84will call 85.Fn sched_nextlwp 86to select a new LWP from the scheduler's runqueue structures. 87If no runnable LWP is found, the idle LWP is used. 88If the new LWP is not equal to the current one, 89.Fn mi_switch 90will hand over control to the machine-dependent function 91.Xr cpu_switchto 9 92to switch to the new LWP. 93.Pp 94.Fn mi_switch 95has to be called with the LWP lock held 96(through calling 97.Fn lwp_lock 98first) and at the 99.Xr splsched 9 100interrupt protection level. 101It returns with the LWP lock released. 102.Sh RETURN VALUES 103.Fn mi_switch 104returns 1 if a context switch was performed to a 105different LWP, 0 otherwise. 106.Sh SEE ALSO 107.Xr condvar 9 , 108.Xr cpu_switchto 9 , 109.Xr csf 9 , 110.Xr pmap 9 , 111.Xr ras 9 , 112.Xr sched_4bsd 9 , 113.Xr splsched 9 114