Split monolithic /etc/pam.conf into separate files for each service
[dragonfly.git] / sys / kern / lwkt_ipiq.c
CommitLineData
3b6b7bd1 1/*
8c10bfcf
MD
2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
3b6b7bd1
MD
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
8c10bfcf 10 *
3b6b7bd1
MD
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
8c10bfcf
MD
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3b6b7bd1 32 * SUCH DAMAGE.
8c10bfcf 33 *
e8f15168 34 * $DragonFly: src/sys/kern/lwkt_ipiq.c,v 1.14 2005/07/20 20:14:33 dillon Exp $
3b6b7bd1
MD
35 */
36
37/*
38 * This module implements IPI message queueing and the MI portion of IPI
39 * message processing.
40 */
41
42#ifdef _KERNEL
43
e8f15168
MD
44#include "opt_ddb.h"
45
3b6b7bd1
MD
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/kernel.h>
49#include <sys/proc.h>
50#include <sys/rtprio.h>
51#include <sys/queue.h>
52#include <sys/thread2.h>
53#include <sys/sysctl.h>
ac72c7f4 54#include <sys/ktr.h>
3b6b7bd1
MD
55#include <sys/kthread.h>
56#include <machine/cpu.h>
57#include <sys/lock.h>
58#include <sys/caps.h>
59
60#include <vm/vm.h>
61#include <vm/vm_param.h>
62#include <vm/vm_kern.h>
63#include <vm/vm_object.h>
64#include <vm/vm_page.h>
65#include <vm/vm_map.h>
66#include <vm/vm_pager.h>
67#include <vm/vm_extern.h>
68#include <vm/vm_zone.h>
69
70#include <machine/stdarg.h>
71#include <machine/ipl.h>
72#include <machine/smp.h>
73#include <machine/atomic.h>
74
75#define THREAD_STACK (UPAGES * PAGE_SIZE)
76
77#else
78
79#include <sys/stdint.h>
80#include <libcaps/thread.h>
81#include <sys/thread.h>
82#include <sys/msgport.h>
83#include <sys/errno.h>
84#include <libcaps/globaldata.h>
7e8303ad 85#include <machine/cpufunc.h>
3b6b7bd1
MD
86#include <sys/thread2.h>
87#include <sys/msgport2.h>
88#include <stdio.h>
89#include <stdlib.h>
90#include <string.h>
3b6b7bd1 91#include <machine/lock.h>
5e940450
MD
92#include <machine/cpu.h>
93#include <machine/atomic.h>
3b6b7bd1
MD
94
95#endif
96
97#ifdef SMP
4c9f5a7f
MD
98static __int64_t ipiq_count; /* total calls to lwkt_send_ipiq*() */
99static __int64_t ipiq_fifofull; /* number of fifo full conditions detected */
100static __int64_t ipiq_avoided; /* interlock with target avoids cpu ipi */
101static __int64_t ipiq_passive; /* passive IPI messages */
102static __int64_t ipiq_cscount; /* number of cpu synchronizations */
103static int ipiq_optimized = 1; /* XXX temporary sysctl */
e8f15168
MD
104#ifdef PANIC_DEBUG
105static int panic_ipiq_cpu = -1;
106static int panic_ipiq_count = 100;
107#endif
3b6b7bd1
MD
108#endif
109
110#ifdef _KERNEL
111
112#ifdef SMP
113SYSCTL_QUAD(_lwkt, OID_AUTO, ipiq_count, CTLFLAG_RW, &ipiq_count, 0, "");
114SYSCTL_QUAD(_lwkt, OID_AUTO, ipiq_fifofull, CTLFLAG_RW, &ipiq_fifofull, 0, "");
4c9f5a7f
MD
115SYSCTL_QUAD(_lwkt, OID_AUTO, ipiq_avoided, CTLFLAG_RW, &ipiq_avoided, 0, "");
116SYSCTL_QUAD(_lwkt, OID_AUTO, ipiq_passive, CTLFLAG_RW, &ipiq_passive, 0, "");
0f7a3396 117SYSCTL_QUAD(_lwkt, OID_AUTO, ipiq_cscount, CTLFLAG_RW, &ipiq_cscount, 0, "");
4c9f5a7f 118SYSCTL_INT(_lwkt, OID_AUTO, ipiq_optimized, CTLFLAG_RW, &ipiq_optimized, 0, "");
e8f15168
MD
119#ifdef PANIC_DEBUG
120SYSCTL_INT(_lwkt, OID_AUTO, panic_ipiq_cpu, CTLFLAG_RW, &panic_ipiq_cpu, 0, "");
121SYSCTL_INT(_lwkt, OID_AUTO, panic_ipiq_count, CTLFLAG_RW, &panic_ipiq_count, 0, "");
122#endif
3b6b7bd1 123
ac72c7f4
MD
124#define IPIQ_STRING "func=%p arg=%p scpu=%d dcpu=%d"
125#define IPIQ_ARG_SIZE (sizeof(void *) * 2 + sizeof(int) * 2)
126
127#if !defined(KTR_IPIQ)
128#define KTR_IPIQ KTR_ALL
3b6b7bd1 129#endif
ac72c7f4
MD
130KTR_INFO_MASTER(ipiq);
131KTR_INFO(KTR_IPIQ, ipiq, send_norm, 0, IPIQ_STRING, IPIQ_ARG_SIZE);
132KTR_INFO(KTR_IPIQ, ipiq, send_pasv, 1, IPIQ_STRING, IPIQ_ARG_SIZE);
133KTR_INFO(KTR_IPIQ, ipiq, send_nbio, 2, IPIQ_STRING, IPIQ_ARG_SIZE);
134KTR_INFO(KTR_IPIQ, ipiq, send_fail, 3, IPIQ_STRING, IPIQ_ARG_SIZE);
135KTR_INFO(KTR_IPIQ, ipiq, receive, 4, IPIQ_STRING, IPIQ_ARG_SIZE);
136
137#define logipiq(name, func, arg, sgd, dgd) \
138 KTR_LOG(ipiq_ ## name, func, arg, sgd->gd_cpuid, dgd->gd_cpuid)
139
140#endif /* SMP */
141#endif /* KERNEL */
3b6b7bd1
MD
142
143#ifdef SMP
144
ac72c7f4 145static int lwkt_process_ipiq1(globaldata_t sgd, lwkt_ipiq_t ip, struct intrframe *frame);
3b6b7bd1
MD
146static void lwkt_cpusync_remote1(lwkt_cpusync_t poll);
147static void lwkt_cpusync_remote2(lwkt_cpusync_t poll);
148
149/*
150 * Send a function execution request to another cpu. The request is queued
151 * on the cpu<->cpu ipiq matrix. Each cpu owns a unique ipiq FIFO for every
152 * possible target cpu. The FIFO can be written.
153 *
4c9f5a7f
MD
154 * If the FIFO fills up we have to enable interrupts to avoid an APIC
155 * deadlock and process pending IPIQs while waiting for it to empty.
156 * Otherwise we may soft-deadlock with another cpu whos FIFO is also full.
3b6b7bd1
MD
157 *
158 * We can safely bump gd_intr_nesting_level because our crit_exit() at the
159 * end will take care of any pending interrupts.
160 *
4c9f5a7f
MD
161 * The actual hardware IPI is avoided if the target cpu is already processing
162 * the queue from a prior IPI. It is possible to pipeline IPI messages
163 * very quickly between cpus due to the FIFO hysteresis.
164 *
165 * Need not be called from a critical section.
3b6b7bd1
MD
166 */
167int
168lwkt_send_ipiq(globaldata_t target, ipifunc_t func, void *arg)
169{
170 lwkt_ipiq_t ip;
171 int windex;
172 struct globaldata *gd = mycpu;
173
ac72c7f4
MD
174 logipiq(send_norm, func, arg, gd, target);
175
3b6b7bd1
MD
176 if (target == gd) {
177 func(arg);
178 return(0);
179 }
180 crit_enter();
181 ++gd->gd_intr_nesting_level;
182#ifdef INVARIANTS
183 if (gd->gd_intr_nesting_level > 20)
184 panic("lwkt_send_ipiq: TOO HEAVILY NESTED!");
185#endif
186 KKASSERT(curthread->td_pri >= TDPRI_CRIT);
187 ++ipiq_count;
188 ip = &gd->gd_ipiq[target->gd_cpuid];
189
190 /*
4c9f5a7f
MD
191 * Do not allow the FIFO to become full. Interrupts must be physically
192 * enabled while we liveloop to avoid deadlocking the APIC.
193 */
194 if (ip->ip_windex - ip->ip_rindex > MAXCPUFIFO / 2) {
195 unsigned int eflags = read_eflags();
196
197 if (atomic_poll_acquire_int(&ip->ip_npoll) || ipiq_optimized == 0)
198 cpu_send_ipiq(target->gd_cpuid);
199 cpu_enable_intr();
200 ++ipiq_fifofull;
201 while (ip->ip_windex - ip->ip_rindex > MAXCPUFIFO / 4) {
202 KKASSERT(ip->ip_windex - ip->ip_rindex != MAXCPUFIFO - 1);
203 lwkt_process_ipiq();
204 }
205 write_eflags(eflags);
206 }
207
208 /*
209 * Queue the new message
3b6b7bd1 210 */
3b6b7bd1
MD
211 windex = ip->ip_windex & MAXCPUFIFO_MASK;
212 ip->ip_func[windex] = (ipifunc2_t)func;
213 ip->ip_arg[windex] = arg;
35238fa5 214 cpu_sfence();
3b6b7bd1 215 ++ip->ip_windex;
4c9f5a7f
MD
216 --gd->gd_intr_nesting_level;
217
218 /*
219 * signal the target cpu that there is work pending.
220 */
221 if (atomic_poll_acquire_int(&ip->ip_npoll)) {
222 cpu_send_ipiq(target->gd_cpuid);
223 } else {
224 if (ipiq_optimized == 0)
225 cpu_send_ipiq(target->gd_cpuid);
226 ++ipiq_avoided;
227 }
228 crit_exit();
229 return(ip->ip_windex);
230}
231
232/*
233 * Similar to lwkt_send_ipiq() but this function does not actually initiate
234 * the IPI to the target cpu unless the FIFO has become too full, so it is
235 * very fast.
236 *
237 * This function is used for non-critical IPI messages, such as memory
238 * deallocations. The queue will typically be flushed by the target cpu at
239 * the next clock interrupt.
240 *
241 * Need not be called from a critical section.
242 */
243int
244lwkt_send_ipiq_passive(globaldata_t target, ipifunc_t func, void *arg)
245{
246 lwkt_ipiq_t ip;
247 int windex;
248 struct globaldata *gd = mycpu;
249
250 KKASSERT(target != gd);
251 crit_enter();
ac72c7f4 252 logipiq(send_pasv, func, arg, gd, target);
4c9f5a7f
MD
253 ++gd->gd_intr_nesting_level;
254#ifdef INVARIANTS
255 if (gd->gd_intr_nesting_level > 20)
256 panic("lwkt_send_ipiq: TOO HEAVILY NESTED!");
257#endif
258 KKASSERT(curthread->td_pri >= TDPRI_CRIT);
259 ++ipiq_count;
260 ++ipiq_passive;
261 ip = &gd->gd_ipiq[target->gd_cpuid];
262
263 /*
264 * Do not allow the FIFO to become full. Interrupts must be physically
265 * enabled while we liveloop to avoid deadlocking the APIC.
266 */
3b6b7bd1
MD
267 if (ip->ip_windex - ip->ip_rindex > MAXCPUFIFO / 2) {
268 unsigned int eflags = read_eflags();
4c9f5a7f
MD
269
270 if (atomic_poll_acquire_int(&ip->ip_npoll) || ipiq_optimized == 0)
271 cpu_send_ipiq(target->gd_cpuid);
3b6b7bd1
MD
272 cpu_enable_intr();
273 ++ipiq_fifofull;
274 while (ip->ip_windex - ip->ip_rindex > MAXCPUFIFO / 4) {
275 KKASSERT(ip->ip_windex - ip->ip_rindex != MAXCPUFIFO - 1);
276 lwkt_process_ipiq();
277 }
278 write_eflags(eflags);
279 }
4c9f5a7f
MD
280
281 /*
282 * Queue the new message
283 */
284 windex = ip->ip_windex & MAXCPUFIFO_MASK;
285 ip->ip_func[windex] = (ipifunc2_t)func;
286 ip->ip_arg[windex] = arg;
35238fa5 287 cpu_sfence();
4c9f5a7f 288 ++ip->ip_windex;
3b6b7bd1 289 --gd->gd_intr_nesting_level;
4c9f5a7f
MD
290
291 /*
292 * Do not signal the target cpu, it will pick up the IPI when it next
293 * polls (typically on the next tick).
294 */
3b6b7bd1
MD
295 crit_exit();
296 return(ip->ip_windex);
297}
298
41a01a4d 299/*
4c9f5a7f
MD
300 * Send an IPI request without blocking, return 0 on success, ENOENT on
301 * failure. The actual queueing of the hardware IPI may still force us
302 * to spin and process incoming IPIs but that will eventually go away
303 * when we've gotten rid of the other general IPIs.
41a01a4d
MD
304 */
305int
4c9f5a7f 306lwkt_send_ipiq_nowait(globaldata_t target, ipifunc_t func, void *arg)
41a01a4d
MD
307{
308 lwkt_ipiq_t ip;
309 int windex;
310 struct globaldata *gd = mycpu;
311
ac72c7f4 312 logipiq(send_nbio, func, arg, gd, target);
41a01a4d
MD
313 KKASSERT(curthread->td_pri >= TDPRI_CRIT);
314 if (target == gd) {
315 func(arg);
316 return(0);
317 }
318 ++ipiq_count;
319 ip = &gd->gd_ipiq[target->gd_cpuid];
320
ac72c7f4
MD
321 if (ip->ip_windex - ip->ip_rindex >= MAXCPUFIFO * 2 / 3) {
322 logipiq(send_fail, func, arg, gd, target);
41a01a4d 323 return(ENOENT);
ac72c7f4 324 }
41a01a4d
MD
325 windex = ip->ip_windex & MAXCPUFIFO_MASK;
326 ip->ip_func[windex] = (ipifunc2_t)func;
327 ip->ip_arg[windex] = arg;
35238fa5 328 cpu_sfence();
41a01a4d 329 ++ip->ip_windex;
4c9f5a7f 330
41a01a4d 331 /*
4c9f5a7f 332 * This isn't a passive IPI, we still have to signal the target cpu.
41a01a4d 333 */
4c9f5a7f
MD
334 if (atomic_poll_acquire_int(&ip->ip_npoll)) {
335 cpu_send_ipiq(target->gd_cpuid);
336 } else {
337 if (ipiq_optimized == 0)
338 cpu_send_ipiq(target->gd_cpuid);
728f6208
MD
339 else
340 ++ipiq_avoided;
4c9f5a7f 341 }
41a01a4d
MD
342 return(0);
343}
344
3b6b7bd1
MD
345/*
346 * deprecated, used only by fast int forwarding.
347 */
348int
349lwkt_send_ipiq_bycpu(int dcpu, ipifunc_t func, void *arg)
350{
351 return(lwkt_send_ipiq(globaldata_find(dcpu), func, arg));
352}
353
354/*
355 * Send a message to several target cpus. Typically used for scheduling.
356 * The message will not be sent to stopped cpus.
357 */
358int
359lwkt_send_ipiq_mask(u_int32_t mask, ipifunc_t func, void *arg)
360{
361 int cpuid;
362 int count = 0;
363
364 mask &= ~stopped_cpus;
365 while (mask) {
366 cpuid = bsfl(mask);
367 lwkt_send_ipiq(globaldata_find(cpuid), func, arg);
368 mask &= ~(1 << cpuid);
369 ++count;
370 }
371 return(count);
372}
373
374/*
375 * Wait for the remote cpu to finish processing a function.
376 *
377 * YYY we have to enable interrupts and process the IPIQ while waiting
378 * for it to empty or we may deadlock with another cpu. Create a CPU_*()
379 * function to do this! YYY we really should 'block' here.
380 *
381 * MUST be called from a critical section. This routine may be called
382 * from an interrupt (for example, if an interrupt wakes a foreign thread
383 * up).
384 */
385void
386lwkt_wait_ipiq(globaldata_t target, int seq)
387{
388 lwkt_ipiq_t ip;
389 int maxc = 100000000;
390
391 if (target != mycpu) {
392 ip = &mycpu->gd_ipiq[target->gd_cpuid];
393 if ((int)(ip->ip_xindex - seq) < 0) {
394 unsigned int eflags = read_eflags();
395 cpu_enable_intr();
396 while ((int)(ip->ip_xindex - seq) < 0) {
41a01a4d 397 crit_enter();
3b6b7bd1 398 lwkt_process_ipiq();
41a01a4d 399 crit_exit();
3b6b7bd1
MD
400 if (--maxc == 0)
401 printf("LWKT_WAIT_IPIQ WARNING! %d wait %d (%d)\n", mycpu->gd_cpuid, target->gd_cpuid, ip->ip_xindex - seq);
402 if (maxc < -1000000)
403 panic("LWKT_WAIT_IPIQ");
35238fa5
MD
404 /*
405 * xindex may be modified by another cpu, use a load fence
406 * to ensure that the loop does not use a speculative value
407 * (which may improve performance).
408 */
409 cpu_lfence();
3b6b7bd1
MD
410 }
411 write_eflags(eflags);
412 }
413 }
414}
415
41a01a4d
MD
416int
417lwkt_seq_ipiq(globaldata_t target)
418{
419 lwkt_ipiq_t ip;
420
421 ip = &mycpu->gd_ipiq[target->gd_cpuid];
422 return(ip->ip_windex);
423}
424
3b6b7bd1
MD
425/*
426 * Called from IPI interrupt (like a fast interrupt), which has placed
427 * us in a critical section. The MP lock may or may not be held.
428 * May also be called from doreti or splz, or be reentrantly called
429 * indirectly through the ip_func[] we run.
430 *
431 * There are two versions, one where no interrupt frame is available (when
432 * called from the send code and from splz, and one where an interrupt
433 * frame is available.
434 */
435void
436lwkt_process_ipiq(void)
437{
438 globaldata_t gd = mycpu;
ac72c7f4 439 globaldata_t sgd;
3b6b7bd1
MD
440 lwkt_ipiq_t ip;
441 int n;
442
443again:
444 for (n = 0; n < ncpus; ++n) {
445 if (n != gd->gd_cpuid) {
ac72c7f4
MD
446 sgd = globaldata_find(n);
447 ip = sgd->gd_ipiq;
3b6b7bd1 448 if (ip != NULL) {
ac72c7f4 449 while (lwkt_process_ipiq1(sgd, &ip[gd->gd_cpuid], NULL))
3b6b7bd1
MD
450 ;
451 }
452 }
453 }
454 if (gd->gd_cpusyncq.ip_rindex != gd->gd_cpusyncq.ip_windex) {
ac72c7f4 455 if (lwkt_process_ipiq1(gd, &gd->gd_cpusyncq, NULL)) {
0f7a3396
MD
456 if (gd->gd_curthread->td_cscount == 0)
457 goto again;
458 need_ipiq();
459 }
3b6b7bd1
MD
460 }
461}
462
463#ifdef _KERNEL
464void
465lwkt_process_ipiq_frame(struct intrframe frame)
466{
467 globaldata_t gd = mycpu;
ac72c7f4 468 globaldata_t sgd;
3b6b7bd1
MD
469 lwkt_ipiq_t ip;
470 int n;
471
472again:
473 for (n = 0; n < ncpus; ++n) {
474 if (n != gd->gd_cpuid) {
ac72c7f4
MD
475 sgd = globaldata_find(n);
476 ip = sgd->gd_ipiq;
3b6b7bd1 477 if (ip != NULL) {
ac72c7f4 478 while (lwkt_process_ipiq1(sgd, &ip[gd->gd_cpuid], &frame))
3b6b7bd1
MD
479 ;
480 }
481 }
482 }
483 if (gd->gd_cpusyncq.ip_rindex != gd->gd_cpusyncq.ip_windex) {
ac72c7f4 484 if (lwkt_process_ipiq1(gd, &gd->gd_cpusyncq, &frame)) {
0f7a3396
MD
485 if (gd->gd_curthread->td_cscount == 0)
486 goto again;
487 need_ipiq();
488 }
3b6b7bd1
MD
489 }
490}
491#endif
492
493static int
ac72c7f4 494lwkt_process_ipiq1(globaldata_t sgd, lwkt_ipiq_t ip, struct intrframe *frame)
3b6b7bd1
MD
495{
496 int ri;
35238fa5 497 int wi;
728f6208
MD
498 void (*copy_func)(void *data, struct intrframe *frame);
499 void *copy_arg;
35238fa5
MD
500
501 /*
502 * Obtain the current write index, which is modified by a remote cpu.
503 * Issue a load fence to prevent speculative reads of e.g. data written
504 * by the other cpu prior to it updating the index.
505 */
728f6208 506 KKASSERT(curthread->td_pri >= TDPRI_CRIT);
35238fa5
MD
507 wi = ip->ip_windex;
508 cpu_lfence();
509
3b6b7bd1
MD
510 /*
511 * Note: xindex is only updated after we are sure the function has
512 * finished execution. Beware lwkt_process_ipiq() reentrancy! The
513 * function may send an IPI which may block/drain.
514 */
515 while ((ri = ip->ip_rindex) != wi) {
3b6b7bd1 516 ri &= MAXCPUFIFO_MASK;
728f6208
MD
517 copy_func = ip->ip_func[ri];
518 copy_arg = ip->ip_arg[ri];
519 cpu_mfence();
520 ++ip->ip_rindex;
521 KKASSERT((ip->ip_rindex & MAXCPUFIFO_MASK) == ((ri + 1) & MAXCPUFIFO_MASK));
ac72c7f4 522 logipiq(receive, copy_func, copy_arg, sgd, mycpu);
728f6208 523 copy_func(copy_arg, frame);
35238fa5 524 cpu_sfence();
3b6b7bd1 525 ip->ip_xindex = ip->ip_rindex;
e8f15168
MD
526
527#ifdef PANIC_DEBUG
528 /*
529 * Simulate panics during the processing of an IPI
530 */
531 if (mycpu->gd_cpuid == panic_ipiq_cpu && panic_ipiq_count) {
532 if (--panic_ipiq_count == 0) {
533#ifdef DDB
534 Debugger("PANIC_DEBUG");
535#else
536 panic("PANIC_DEBUG");
537#endif
538 }
539 }
540#endif
3b6b7bd1 541 }
4c9f5a7f
MD
542
543 /*
544 * Return non-zero if there are more IPI messages pending on this
545 * ipiq. ip_npoll is left set as long as possible to reduce the
546 * number of IPIs queued by the originating cpu, but must be cleared
547 * *BEFORE* checking windex.
548 */
549 atomic_poll_release_int(&ip->ip_npoll);
3b6b7bd1
MD
550 return(wi != ip->ip_windex);
551}
552
0f7a3396
MD
553#else
554
555/*
556 * !SMP dummy routines
557 */
558
559int
560lwkt_send_ipiq(globaldata_t target, ipifunc_t func, void *arg)
561{
562 panic("lwkt_send_ipiq: UP box! (%d,%p,%p)", target->gd_cpuid, func, arg);
563 return(0); /* NOT REACHED */
564}
565
566void
567lwkt_wait_ipiq(globaldata_t target, int seq)
568{
569 panic("lwkt_wait_ipiq: UP box! (%d,%d)", target->gd_cpuid, seq);
570}
571
572#endif
573
3b6b7bd1
MD
574/*
575 * CPU Synchronization Support
5c71a36a
MD
576 *
577 * lwkt_cpusync_simple()
578 *
579 * The function is executed synchronously before return on remote cpus.
580 * A lwkt_cpusync_t pointer is passed as an argument. The data can
581 * be accessed via arg->cs_data.
582 *
583 * XXX should I just pass the data as an argument to be consistent?
3b6b7bd1
MD
584 */
585
586void
5c71a36a
MD
587lwkt_cpusync_simple(cpumask_t mask, cpusync_func_t func, void *data)
588{
589 struct lwkt_cpusync cmd;
590
591 cmd.cs_run_func = NULL;
592 cmd.cs_fin1_func = func;
593 cmd.cs_fin2_func = NULL;
594 cmd.cs_data = data;
595 lwkt_cpusync_start(mask & mycpu->gd_other_cpus, &cmd);
596 if (mask & (1 << mycpu->gd_cpuid))
597 func(&cmd);
598 lwkt_cpusync_finish(&cmd);
599}
600
601/*
602 * lwkt_cpusync_fastdata()
603 *
604 * The function is executed in tandem with return on remote cpus.
605 * The data is directly passed as an argument. Do not pass pointers to
606 * temporary storage as the storage might have
607 * gone poof by the time the target cpu executes
608 * the function.
609 *
610 * At the moment lwkt_cpusync is declared on the stack and we must wait
611 * for all remote cpus to ack in lwkt_cpusync_finish(), but as a future
612 * optimization we should be able to put a counter in the globaldata
613 * structure (if it is not otherwise being used) and just poke it and
614 * return without waiting. XXX
615 */
616void
617lwkt_cpusync_fastdata(cpumask_t mask, cpusync_func2_t func, void *data)
3b6b7bd1
MD
618{
619 struct lwkt_cpusync cmd;
3b6b7bd1
MD
620
621 cmd.cs_run_func = NULL;
622 cmd.cs_fin1_func = NULL;
623 cmd.cs_fin2_func = func;
5c71a36a
MD
624 cmd.cs_data = NULL;
625 lwkt_cpusync_start(mask & mycpu->gd_other_cpus, &cmd);
3b6b7bd1
MD
626 if (mask & (1 << mycpu->gd_cpuid))
627 func(data);
5c71a36a 628 lwkt_cpusync_finish(&cmd);
3b6b7bd1
MD
629}
630
631/*
5c71a36a
MD
632 * lwkt_cpusync_start()
633 *
634 * Start synchronization with a set of target cpus, return once they are
635 * known to be in a synchronization loop. The target cpus will execute
636 * poll->cs_run_func() IN TANDEM WITH THE RETURN.
637 *
638 * XXX future: add lwkt_cpusync_start_quick() and require a call to
639 * lwkt_cpusync_add() or lwkt_cpusync_wait(), allowing the caller to
640 * potentially absorb the IPI latency doing something useful.
3b6b7bd1 641 */
5c71a36a 642void
3b6b7bd1
MD
643lwkt_cpusync_start(cpumask_t mask, lwkt_cpusync_t poll)
644{
0f7a3396
MD
645 globaldata_t gd = mycpu;
646
3b6b7bd1 647 poll->cs_count = 0;
5c71a36a 648 poll->cs_mask = mask;
0f7a3396
MD
649#ifdef SMP
650 poll->cs_maxcount = lwkt_send_ipiq_mask(
651 mask & gd->gd_other_cpus & smp_active_mask,
652 (ipifunc_t)lwkt_cpusync_remote1, poll);
653#endif
fda1ad89 654 if (mask & gd->gd_cpumask) {
5c71a36a
MD
655 if (poll->cs_run_func)
656 poll->cs_run_func(poll);
657 }
0f7a3396
MD
658#ifdef SMP
659 if (poll->cs_maxcount) {
660 ++ipiq_cscount;
661 ++gd->gd_curthread->td_cscount;
662 while (poll->cs_count != poll->cs_maxcount) {
663 crit_enter();
664 lwkt_process_ipiq();
665 crit_exit();
666 }
5c71a36a 667 }
0f7a3396 668#endif
5c71a36a
MD
669}
670
671void
672lwkt_cpusync_add(cpumask_t mask, lwkt_cpusync_t poll)
673{
0f7a3396 674 globaldata_t gd = mycpu;
41a01a4d 675#ifdef SMP
0f7a3396 676 int count;
41a01a4d 677#endif
0f7a3396 678
5c71a36a
MD
679 mask &= ~poll->cs_mask;
680 poll->cs_mask |= mask;
0f7a3396
MD
681#ifdef SMP
682 count = lwkt_send_ipiq_mask(
683 mask & gd->gd_other_cpus & smp_active_mask,
684 (ipifunc_t)lwkt_cpusync_remote1, poll);
685#endif
fda1ad89 686 if (mask & gd->gd_cpumask) {
5c71a36a
MD
687 if (poll->cs_run_func)
688 poll->cs_run_func(poll);
689 }
0f7a3396
MD
690#ifdef SMP
691 poll->cs_maxcount += count;
692 if (poll->cs_maxcount) {
693 if (poll->cs_maxcount == count)
694 ++gd->gd_curthread->td_cscount;
695 while (poll->cs_count != poll->cs_maxcount) {
696 crit_enter();
697 lwkt_process_ipiq();
698 crit_exit();
699 }
3b6b7bd1 700 }
0f7a3396 701#endif
3b6b7bd1
MD
702}
703
704/*
705 * Finish synchronization with a set of target cpus. The target cpus will
706 * execute cs_fin1_func(poll) prior to this function returning, and will
707 * execute cs_fin2_func(data) IN TANDEM WITH THIS FUNCTION'S RETURN.
0f7a3396
MD
708 *
709 * If cs_maxcount is non-zero then we are mastering a cpusync with one or
710 * more remote cpus and must account for it in our thread structure.
3b6b7bd1
MD
711 */
712void
5c71a36a 713lwkt_cpusync_finish(lwkt_cpusync_t poll)
3b6b7bd1 714{
0f7a3396 715 globaldata_t gd = mycpu;
5c71a36a 716
3b6b7bd1 717 poll->cs_count = -1;
fda1ad89 718 if (poll->cs_mask & gd->gd_cpumask) {
5c71a36a
MD
719 if (poll->cs_fin1_func)
720 poll->cs_fin1_func(poll);
721 if (poll->cs_fin2_func)
722 poll->cs_fin2_func(poll->cs_data);
723 }
0f7a3396
MD
724#ifdef SMP
725 if (poll->cs_maxcount) {
726 while (poll->cs_count != -(poll->cs_maxcount + 1)) {
727 crit_enter();
728 lwkt_process_ipiq();
729 crit_exit();
730 }
731 --gd->gd_curthread->td_cscount;
3b6b7bd1 732 }
0f7a3396 733#endif
3b6b7bd1
MD
734}
735
0f7a3396
MD
736#ifdef SMP
737
3b6b7bd1
MD
738/*
739 * helper IPI remote messaging function.
740 *
741 * Called on remote cpu when a new cpu synchronization request has been
742 * sent to us. Execute the run function and adjust cs_count, then requeue
743 * the request so we spin on it.
744 */
745static void
746lwkt_cpusync_remote1(lwkt_cpusync_t poll)
747{
748 atomic_add_int(&poll->cs_count, 1);
749 if (poll->cs_run_func)
750 poll->cs_run_func(poll);
751 lwkt_cpusync_remote2(poll);
752}
753
754/*
755 * helper IPI remote messaging function.
756 *
757 * Poll for the originator telling us to finish. If it hasn't, requeue
758 * our request so we spin on it. When the originator requests that we
759 * finish we execute cs_fin1_func(poll) synchronously and cs_fin2_func(data)
760 * in tandem with the release.
761 */
762static void
763lwkt_cpusync_remote2(lwkt_cpusync_t poll)
764{
765 if (poll->cs_count < 0) {
766 cpusync_func2_t savef;
767 void *saved;
768
769 if (poll->cs_fin1_func)
770 poll->cs_fin1_func(poll);
771 if (poll->cs_fin2_func) {
772 savef = poll->cs_fin2_func;
773 saved = poll->cs_data;
774 atomic_add_int(&poll->cs_count, -1);
775 savef(saved);
776 } else {
777 atomic_add_int(&poll->cs_count, -1);
778 }
779 } else {
780 globaldata_t gd = mycpu;
781 lwkt_ipiq_t ip;
782 int wi;
783
784 ip = &gd->gd_cpusyncq;
785 wi = ip->ip_windex & MAXCPUFIFO_MASK;
786 ip->ip_func[wi] = (ipifunc2_t)lwkt_cpusync_remote2;
787 ip->ip_arg[wi] = poll;
35238fa5 788 cpu_sfence();
3b6b7bd1
MD
789 ++ip->ip_windex;
790 }
791}
792
3b6b7bd1 793#endif