1.\" $OpenBSD: mi_switch.9,v 1.7 2017/09/05 03:49:56 jmatthew Exp $ 2.\" $NetBSD: ctxsw.9,v 1.9 1999/03/06 22:09:29 mycroft Exp $ 3.\" 4.\" Copyright (c) 1996 The NetBSD Foundation, Inc. 5.\" All rights reserved. 6.\" 7.\" This code is derived from software contributed to The NetBSD Foundation 8.\" by Paul Kranenburg. 9.\" 10.\" Redistribution and use in source and binary forms, with or without 11.\" modification, are permitted provided that the following conditions 12.\" are met: 13.\" 1. Redistributions of source code must retain the above copyright 14.\" notice, this list of conditions and the following disclaimer. 15.\" 2. Redistributions in binary form must reproduce the above copyright 16.\" notice, this list of conditions and the following disclaimer in the 17.\" documentation and/or other materials provided with the distribution. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29.\" POSSIBILITY OF SUCH DAMAGE. 30.\" 31.Dd $Mdocdate: September 5 2017 $ 32.Dt MI_SWITCH 9 33.Os 34.Sh NAME 35.Nm mi_switch , 36.Nm cpu_switchto 37.Nd switch to another process context 38.Sh SYNOPSIS 39.In sys/param.h 40.In sys/proc.h 41.Ft void 42.Fn mi_switch "void" 43.Ft void 44.Fn cpu_switchto "struct proc *old" "struct proc *new" 45.Sh DESCRIPTION 46The 47.Fn mi_switch 48function implements the machine-independent prelude to a process context 49switch. 50It is called from only a few distinguished places in the kernel code as a 51result of the principle of non-preemptable kernel mode execution. 52The three major uses of 53.Fn mi_switch 54can be enumerated as follows: 55.Bl -enum -offset indent 56.It 57From within functions like 58.Xr tsleep 9 , 59when the current process sleeps to wait for some resource to become 60available, or from within functions like 61.Fn yield , 62when it relinquishes the CPU to allow other processes to make progress. 63.It 64After handling a trap 65.Pq e.g., a system call or device interrupt 66when the kernel prepares a return to user-mode execution. 67This case is typically handled by machine-dependent trap-handling code after 68detection of a change in the signal disposition of the current process, or 69when a higher priority process might be available to run. 70The latter event is communicated by the machine-independent scheduling 71routines by calling the machine-dependent 72.Fn need_resched "void" . 73.It 74In the signal handling code 75.Pq see Pa sys/kern/kern_sig.c 76if a signal is delivered that causes a process to stop. 77.El 78.Pp 79.Fn mi_switch 80records the amount of time the current process has been running in the 81process structure and checks this value against the CPU time limits 82allocated to the process 83.Pq see Xr getrlimit 2 . 84Exceeding the soft limit results in a 85.Dv SIGXCPU 86signal to be posted to the process, while exceeding the hard limit will 87cause a 88.Dv SIGKILL . 89After these administrative tasks are done, 90.Fn mi_switch 91chooses the next process to run and hands over control to the machine 92dependent routine 93.Fn cpu_switchto , 94which will perform the actual process context switch. 95.Pp 96.Fn cpu_switchto 97will save the context of the old process and switch to the new one. 98A special case is when the old process is 99.Dv NULL 100which means that the old process has exited and doesn't need to be 101saved. 102.Pp 103Note that 104.Fn mi_switch 105and thus 106.Fn cpu_switchto 107must be called while holding the scheduler lock. 108.Sh SEE ALSO 109.Xr spl 9 , 110.Xr tsleep 9 , 111.Xr wakeup 9 112