1.\" $OpenBSD: mi_switch.9,v 1.5 2013/06/04 19:27:09 schwarze 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: June 4 2013 $ 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 58.Xr tsleep 9 59when the current process 60voluntarily relinquishes the CPU to wait for some resource to become 61available. 62.It 63After handling a trap 64.Pq e.g., a system call or device interrupt 65when the kernel prepares a return to user-mode execution. 66This case is typically handled by machine-dependent trap-handling code after 67detection of a change in the signal disposition of the current process, or 68when a higher priority process might be available to run. 69The latter event is communicated by the machine-independent scheduling 70routines by calling the machine-dependent 71.Fn need_resched "void" . 72.It 73In the signal handling code 74.Pq see Xr issignal 9 75if a signal is delivered that causes a process to stop. 76.El 77.Pp 78.Fn mi_switch 79records the amount of time the current process has been running in the 80process structure and checks this value against the CPU time limits 81allocated to the process 82.Pq see Xr getrlimit 2 . 83Exceeding the soft limit results in a 84.Dv SIGXCPU 85signal to be posted to the process, while exceeding the hard limit will 86cause a 87.Dv SIGKILL . 88For a process which accumulated longer than 10 minutes of 89CPU time, its nice level is raised to 4. 90After these administrative tasks are done, 91.Fn mi_switch 92chooses the next process to run and hands over control to the machine 93dependent routine 94.Fn cpu_switchto , 95which will perform the actual process context switch. 96.Pp 97.Fn cpu_switchto 98will save the context of the old process and switch to the new one. 99A special case is when the old process is 100.Dv NULL 101which means that the old process has exited and doesn't need to be 102saved. 103.Pp 104Note that 105.Fn mi_switch 106and thus 107.Fn cpu_switchto 108should be called at 109.Xr splhigh 9 . 110.Sh SEE ALSO 111.Xr spl 9 , 112.Xr tsleep 9 , 113.Xr wakeup 9 114