1.\" $NetBSD: xcall.9,v 1.17 2020/02/01 13:35:11 riastradh Exp $ 2.\" 3.\" Copyright (c) 2010 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to The NetBSD Foundation 7.\" by Andrew Doran. 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 February 1, 2020 31.Dt XCALL 9 32.Os 33.Sh NAME 34.Nm xcall , 35.Nm xc_broadcast , 36.Nm xc_unicast , 37.Nm xc_wait , 38.Nm xc_barrier 39.Nd cross-call interface 40.Sh SYNOPSIS 41.In sys/xcall.h 42.Vt typedef void (*xcfunc_t)(void *, void *); 43.Ft uint64_t 44.Fn xc_broadcast "u_int flags" "xcfunc_t func" "void *arg1" "void *arg2" 45.Ft uint64_t 46.Fn xc_unicast "u_int flags" "xcfunc_t func" "void *arg1" "void *arg2" "struct cpu_info *ci" 47.Ft void 48.Fn xc_wait "uint64_t where" 49.Ft void 50.Fn xc_barrier "u_int flags" 51.Sh DESCRIPTION 52The machine-independent 53.Nm 54interface allows any CPU in the system to request that an arbitrary 55function be executed on any other CPU. 56.Pp 57Sometimes it is necessary to modify hardware state that is tied 58directly to individual CPUs 59.Po 60such as a CPU's local timer 61.Pc , 62and these updates can not be done remotely by another CPU. 63The LWP requesting the update may be unable to guarantee that it 64will be running on the CPU where the update must occur, when the 65update occurs. 66.Pp 67Additionally, it is sometimes necessary to modify per-CPU software 68state from a remote CPU. 69Where these update operations are so rare or the access to the 70per-CPU data so frequent that the cost of using locking or atomic 71operations to provide coherency is prohibitive, another way must 72be found. 73.Pp 74Cross calls help to solve these types of problem. 75However, since this facility is heavyweight, it is expected that 76it will not be used often. 77.Pp 78.Nm 79provides a mechanism for making 80.Dq "low priority" 81cross calls. 82The function to be executed runs on the remote CPU within a thread 83context, and not from a software interrupt, so it can ensure that it is 84not interrupting other code running on the CPU, and so has exclusive 85access to the CPU. 86Keep in mind that unless disabled, it may cause a kernel preemption. 87.Pp 88.Nm 89also provides a mechanism for making 90.Dq "high priority" 91cross calls. 92The function to be executed runs on the remote CPU within a 93software interrupt context, possibly interrupting other lower-priority 94code running on the CPU. 95.Sh NOTES 96Functions being called should be relatively lightweight. 97They may block on locks, but carefully and minimally, to not interfere 98with other cross calls in the system. 99.Sh FUNCTIONS 100.Bl -tag -width abcd 101.It Fn xc_broadcast "flags" "func" "arg1" "arg2" 102Call 103.Pf (* Fa func\| ) Ns Fo "" 104.Fa "arg1" 105.Fa "arg2" 106.Fc 107on all CPUs in the system. 108Return a 109.Vt uint64_t 110.Dq ticket 111to 112.Fn xc_wait 113on for the cross-call to complete. 114.Fa flags 115should be 116.Dv XC_HIGHPRI 117or 118.Dv XC_HIGHPRI_IPL\| Ns Fn "" ipl 119for a "high priority" call, and 0 for a "low priority" call. 120.Dv XC_HIGHPRI 121uses an 122.Dv IPL_SOFTSERIAL 123software interrupt while 124.Dv XC_HIGHPRI_IPL 125uses a software interrupt with an IPL specified by 126.Fa ipl . 127.Fn xc_broadcast 128should not be called from interrupt context. 129.It Fn xc_unicast "flags" "func" "arg1" "arg2" "ci" 130Like 131.Fn xc_broadcast , 132but call 133.Fa func 134on only the CPU indicated by 135.Fa ci . 136.Fn xc_unicast 137also returns a 138.Dq ticket . 139.It Fn xc_wait "where" 140Wait on the 141.Dq ticket 142returned by a prior 143.Fn xc_broadcast 144or 145.Fn xc_unicast 146for the corresponding cross-call to complete. 147.Fn xc_wait 148should be called from a thread context. 149.It Fn xc_barrier "flags" 150Issue a broadcast cross-call that does nothing, 151and wait for it to complete. 152.Pp 153This functions like a memory barrier that forces all prior operations 154in program order to globally happen before all subsequent operations in 155program order, as witnessed by every CPU. 156.Pp 157This additionally waits for all higher-priority activity on the CPU to 158complete, according to 159.Fa flags : 160.Bl -dash -compact 161.It 162.Fo xc_barrier 163.Li 0 164.Fc 165waits for any pending 166.Xr kpreempt_disable 9 167sections or activity at interrupt priority level above 168.Dv IPL_NONE 169to finish on all CPUs. 170.It 171.Fo xc_barrier 172.Dv XC_HIGHPRI_IPL\| Ns Fn "" ipl 173.Fc 174waits for any pending activity at the software interrupt priority level 175.Fa ipl 176or higher to finish on all CPUs. 177.El 178.Pp 179.Fn xc_barrier 180is much more expensive than 181.Xr membar_ops 3 , 182so it should be used sparingly, only to publish information 183infrequently \(em for example, during module load and unload \(em when 184the cost of a memory barrier on the consumer side would be 185prohibitive. 186.El 187.Sh CODE REFERENCES 188The 189.Nm 190interface is implemented within the file 191.Pa sys/kern/subr_xcall.c . 192.\" .Sh EXAMPLES 193.Sh SEE ALSO 194.Xr membar_ops 3 , 195.Xr kpreempt 9 , 196.Xr percpu 9 , 197.Xr softint 9 198.Sh HISTORY 199The 200.Nm 201interface first appeared in 202.Nx 5.0 . 203.Sh AUTHORS 204.An Andrew Doran Aq Mt ad@NetBSD.org 205