2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
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.
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
36 * This module implements IPI message queueing and the MI portion of IPI
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/rtprio.h>
47 #include <sys/queue.h>
48 #include <sys/thread2.h>
49 #include <sys/sysctl.h>
51 #include <sys/kthread.h>
52 #include <machine/cpu.h>
56 #include <vm/vm_param.h>
57 #include <vm/vm_kern.h>
58 #include <vm/vm_object.h>
59 #include <vm/vm_page.h>
60 #include <vm/vm_map.h>
61 #include <vm/vm_pager.h>
62 #include <vm/vm_extern.h>
63 #include <vm/vm_zone.h>
65 #include <machine/stdarg.h>
66 #include <machine/smp.h>
67 #include <machine/atomic.h>
69 #ifdef _KERNEL_VIRTUAL
74 __int64_t ipiq_count; /* total calls to lwkt_send_ipiq*() */
75 __int64_t ipiq_fifofull; /* number of fifo full conditions detected */
76 __int64_t ipiq_avoided; /* interlock with target avoids cpu ipi */
77 __int64_t ipiq_passive; /* passive IPI messages */
78 __int64_t ipiq_cscount; /* number of cpu synchronizations */
81 static struct ipiq_stats ipiq_stats_percpu[MAXCPU];
82 #define ipiq_stat(gd) ipiq_stats_percpu[(gd)->gd_cpuid]
84 static int ipiq_debug; /* set to 1 for debug */
86 static int panic_ipiq_cpu = -1;
87 static int panic_ipiq_count = 100;
90 SYSCTL_INT(_lwkt, OID_AUTO, ipiq_debug, CTLFLAG_RW, &ipiq_debug, 0,
93 SYSCTL_INT(_lwkt, OID_AUTO, panic_ipiq_cpu, CTLFLAG_RW, &panic_ipiq_cpu, 0, "");
94 SYSCTL_INT(_lwkt, OID_AUTO, panic_ipiq_count, CTLFLAG_RW, &panic_ipiq_count, 0, "");
97 #define IPIQ_STRING "func=%p arg1=%p arg2=%d scpu=%d dcpu=%d"
98 #define IPIQ_ARGS void *func, void *arg1, int arg2, int scpu, int dcpu
100 #if !defined(KTR_IPIQ)
101 #define KTR_IPIQ KTR_ALL
103 KTR_INFO_MASTER(ipiq);
104 KTR_INFO(KTR_IPIQ, ipiq, send_norm, 0, IPIQ_STRING, IPIQ_ARGS);
105 KTR_INFO(KTR_IPIQ, ipiq, send_pasv, 1, IPIQ_STRING, IPIQ_ARGS);
106 KTR_INFO(KTR_IPIQ, ipiq, send_nbio, 2, IPIQ_STRING, IPIQ_ARGS);
107 KTR_INFO(KTR_IPIQ, ipiq, send_fail, 3, IPIQ_STRING, IPIQ_ARGS);
108 KTR_INFO(KTR_IPIQ, ipiq, receive, 4, IPIQ_STRING, IPIQ_ARGS);
109 KTR_INFO(KTR_IPIQ, ipiq, sync_start, 5, "cpumask=%08lx", unsigned long mask);
110 KTR_INFO(KTR_IPIQ, ipiq, sync_end, 6, "cpumask=%08lx", unsigned long mask);
111 KTR_INFO(KTR_IPIQ, ipiq, cpu_send, 7, IPIQ_STRING, IPIQ_ARGS);
112 KTR_INFO(KTR_IPIQ, ipiq, send_end, 8, IPIQ_STRING, IPIQ_ARGS);
114 #define logipiq(name, func, arg1, arg2, sgd, dgd) \
115 KTR_LOG(ipiq_ ## name, func, arg1, arg2, sgd->gd_cpuid, dgd->gd_cpuid)
116 #define logipiq2(name, arg) \
117 KTR_LOG(ipiq_ ## name, arg)
119 static int lwkt_process_ipiq_core(globaldata_t sgd, lwkt_ipiq_t ip,
120 struct intrframe *frame);
121 static void lwkt_cpusync_remote1(lwkt_cpusync_t cs);
122 static void lwkt_cpusync_remote2(lwkt_cpusync_t cs);
124 #define IPIQ_SYSCTL(name) \
126 sysctl_##name(SYSCTL_HANDLER_ARGS) \
131 for (cpu = 0; cpu < ncpus; ++cpu) \
132 val += ipiq_stats_percpu[cpu].name; \
134 error = sysctl_handle_quad(oidp, &val, 0, req); \
135 if (error || req->newptr == NULL) \
138 for (cpu = 0; cpu < ncpus; ++cpu) \
139 ipiq_stats_percpu[cpu].name = val; \
144 IPIQ_SYSCTL(ipiq_count);
145 IPIQ_SYSCTL(ipiq_fifofull);
146 IPIQ_SYSCTL(ipiq_avoided);
147 IPIQ_SYSCTL(ipiq_passive);
148 IPIQ_SYSCTL(ipiq_cscount);
150 SYSCTL_PROC(_lwkt, OID_AUTO, ipiq_count, (CTLTYPE_QUAD | CTLFLAG_RW),
151 0, 0, sysctl_ipiq_count, "Q", "Number of IPI's sent");
152 SYSCTL_PROC(_lwkt, OID_AUTO, ipiq_fifofull, (CTLTYPE_QUAD | CTLFLAG_RW),
153 0, 0, sysctl_ipiq_fifofull, "Q",
154 "Number of fifo full conditions detected");
155 SYSCTL_PROC(_lwkt, OID_AUTO, ipiq_avoided, (CTLTYPE_QUAD | CTLFLAG_RW),
156 0, 0, sysctl_ipiq_avoided, "Q",
157 "Number of IPI's avoided by interlock with target cpu");
158 SYSCTL_PROC(_lwkt, OID_AUTO, ipiq_passive, (CTLTYPE_QUAD | CTLFLAG_RW),
159 0, 0, sysctl_ipiq_passive, "Q",
160 "Number of passive IPI messages sent");
161 SYSCTL_PROC(_lwkt, OID_AUTO, ipiq_cscount, (CTLTYPE_QUAD | CTLFLAG_RW),
162 0, 0, sysctl_ipiq_cscount, "Q",
163 "Number of cpu synchronizations");
166 * Send a function execution request to another cpu. The request is queued
167 * on the cpu<->cpu ipiq matrix. Each cpu owns a unique ipiq FIFO for every
168 * possible target cpu. The FIFO can be written.
170 * If the FIFO fills up we have to enable interrupts to avoid an APIC
171 * deadlock and process pending IPIQs while waiting for it to empty.
172 * Otherwise we may soft-deadlock with another cpu whos FIFO is also full.
174 * We can safely bump gd_intr_nesting_level because our crit_exit() at the
175 * end will take care of any pending interrupts.
177 * The actual hardware IPI is avoided if the target cpu is already processing
178 * the queue from a prior IPI. It is possible to pipeline IPI messages
179 * very quickly between cpus due to the FIFO hysteresis.
181 * Need not be called from a critical section.
184 lwkt_send_ipiq3(globaldata_t target, ipifunc3_t func, void *arg1, int arg2)
188 struct globaldata *gd = mycpu;
190 logipiq(send_norm, func, arg1, arg2, gd, target);
193 func(arg1, arg2, NULL);
194 logipiq(send_end, func, arg1, arg2, gd, target);
198 ++gd->gd_intr_nesting_level;
200 if (gd->gd_intr_nesting_level > 20)
201 panic("lwkt_send_ipiq: TOO HEAVILY NESTED!");
203 KKASSERT(curthread->td_critcount);
204 ++ipiq_stat(gd).ipiq_count;
205 ip = &gd->gd_ipiq[target->gd_cpuid];
208 * Do not allow the FIFO to become full. Interrupts must be physically
209 * enabled while we liveloop to avoid deadlocking the APIC.
211 * The target ipiq may have gotten filled up due to passive IPIs and thus
212 * not be aware that its queue is too full, so be sure to issue an
213 * ipiq interrupt to the target cpu.
215 if (ip->ip_windex - ip->ip_rindex > MAXCPUFIFO / 2) {
216 #if defined(__i386__)
217 unsigned int eflags = read_eflags();
218 #elif defined(__x86_64__)
219 unsigned long rflags = read_rflags();
223 ++ipiq_stat(gd).ipiq_fifofull;
224 DEBUG_PUSH_INFO("send_ipiq3");
225 while (ip->ip_windex - ip->ip_rindex > MAXCPUFIFO / 4) {
226 if (atomic_poll_acquire_int(&target->gd_npoll)) {
227 logipiq(cpu_send, func, arg1, arg2, gd, target);
228 cpu_send_ipiq(target->gd_cpuid);
230 KKASSERT(ip->ip_windex - ip->ip_rindex != MAXCPUFIFO - 1);
235 #if defined(__i386__)
236 write_eflags(eflags);
237 #elif defined(__x86_64__)
238 write_rflags(rflags);
243 * Queue the new message
245 windex = ip->ip_windex & MAXCPUFIFO_MASK;
246 ip->ip_info[windex].func = func;
247 ip->ip_info[windex].arg1 = arg1;
248 ip->ip_info[windex].arg2 = arg2;
251 atomic_set_cpumask(&target->gd_ipimask, gd->gd_cpumask);
254 * signal the target cpu that there is work pending.
256 if (atomic_poll_acquire_int(&target->gd_npoll)) {
257 logipiq(cpu_send, func, arg1, arg2, gd, target);
258 cpu_send_ipiq(target->gd_cpuid);
260 ++ipiq_stat(gd).ipiq_avoided;
262 --gd->gd_intr_nesting_level;
264 logipiq(send_end, func, arg1, arg2, gd, target);
266 return(ip->ip_windex);
270 * Similar to lwkt_send_ipiq() but this function does not actually initiate
271 * the IPI to the target cpu unless the FIFO has become too full, so it is
274 * This function is used for non-critical IPI messages, such as memory
275 * deallocations. The queue will typically be flushed by the target cpu at
276 * the next clock interrupt.
278 * Need not be called from a critical section.
281 lwkt_send_ipiq3_passive(globaldata_t target, ipifunc3_t func,
282 void *arg1, int arg2)
286 struct globaldata *gd = mycpu;
288 KKASSERT(target != gd);
290 ++gd->gd_intr_nesting_level;
291 logipiq(send_pasv, func, arg1, arg2, gd, target);
293 if (gd->gd_intr_nesting_level > 20)
294 panic("lwkt_send_ipiq: TOO HEAVILY NESTED!");
296 KKASSERT(curthread->td_critcount);
297 ++ipiq_stat(gd).ipiq_count;
298 ++ipiq_stat(gd).ipiq_passive;
299 ip = &gd->gd_ipiq[target->gd_cpuid];
302 * Do not allow the FIFO to become full. Interrupts must be physically
303 * enabled while we liveloop to avoid deadlocking the APIC.
305 if (ip->ip_windex - ip->ip_rindex > MAXCPUFIFO / 2) {
306 #if defined(__i386__)
307 unsigned int eflags = read_eflags();
308 #elif defined(__x86_64__)
309 unsigned long rflags = read_rflags();
313 ++ipiq_stat(gd).ipiq_fifofull;
314 DEBUG_PUSH_INFO("send_ipiq3_passive");
315 while (ip->ip_windex - ip->ip_rindex > MAXCPUFIFO / 4) {
316 if (atomic_poll_acquire_int(&target->gd_npoll)) {
317 logipiq(cpu_send, func, arg1, arg2, gd, target);
318 cpu_send_ipiq(target->gd_cpuid);
320 KKASSERT(ip->ip_windex - ip->ip_rindex != MAXCPUFIFO - 1);
325 #if defined(__i386__)
326 write_eflags(eflags);
327 #elif defined(__x86_64__)
328 write_rflags(rflags);
333 * Queue the new message
335 windex = ip->ip_windex & MAXCPUFIFO_MASK;
336 ip->ip_info[windex].func = func;
337 ip->ip_info[windex].arg1 = arg1;
338 ip->ip_info[windex].arg2 = arg2;
341 atomic_set_cpumask(&target->gd_ipimask, gd->gd_cpumask);
342 --gd->gd_intr_nesting_level;
345 * Do not signal the target cpu, it will pick up the IPI when it next
346 * polls (typically on the next tick).
349 logipiq(send_end, func, arg1, arg2, gd, target);
351 return(ip->ip_windex);
355 * Send an IPI request without blocking, return 0 on success, ENOENT on
356 * failure. The actual queueing of the hardware IPI may still force us
357 * to spin and process incoming IPIs but that will eventually go away
358 * when we've gotten rid of the other general IPIs.
361 lwkt_send_ipiq3_nowait(globaldata_t target, ipifunc3_t func,
362 void *arg1, int arg2)
366 struct globaldata *gd = mycpu;
368 logipiq(send_nbio, func, arg1, arg2, gd, target);
369 KKASSERT(curthread->td_critcount);
371 func(arg1, arg2, NULL);
372 logipiq(send_end, func, arg1, arg2, gd, target);
376 ++gd->gd_intr_nesting_level;
377 ++ipiq_stat(gd).ipiq_count;
378 ip = &gd->gd_ipiq[target->gd_cpuid];
380 if (ip->ip_windex - ip->ip_rindex >= MAXCPUFIFO * 2 / 3) {
381 logipiq(send_fail, func, arg1, arg2, gd, target);
382 --gd->gd_intr_nesting_level;
386 windex = ip->ip_windex & MAXCPUFIFO_MASK;
387 ip->ip_info[windex].func = func;
388 ip->ip_info[windex].arg1 = arg1;
389 ip->ip_info[windex].arg2 = arg2;
392 atomic_set_cpumask(&target->gd_ipimask, gd->gd_cpumask);
395 * This isn't a passive IPI, we still have to signal the target cpu.
397 if (atomic_poll_acquire_int(&target->gd_npoll)) {
398 logipiq(cpu_send, func, arg1, arg2, gd, target);
399 cpu_send_ipiq(target->gd_cpuid);
401 ++ipiq_stat(gd).ipiq_avoided;
403 --gd->gd_intr_nesting_level;
406 logipiq(send_end, func, arg1, arg2, gd, target);
411 * deprecated, used only by fast int forwarding.
414 lwkt_send_ipiq3_bycpu(int dcpu, ipifunc3_t func, void *arg1, int arg2)
416 return(lwkt_send_ipiq3(globaldata_find(dcpu), func, arg1, arg2));
420 * Send a message to several target cpus. Typically used for scheduling.
421 * The message will not be sent to stopped cpus.
424 lwkt_send_ipiq3_mask(cpumask_t mask, ipifunc3_t func, void *arg1, int arg2)
429 mask &= ~stopped_cpus;
431 cpuid = BSFCPUMASK(mask);
432 lwkt_send_ipiq3(globaldata_find(cpuid), func, arg1, arg2);
433 mask &= ~CPUMASK(cpuid);
440 * Wait for the remote cpu to finish processing a function.
442 * YYY we have to enable interrupts and process the IPIQ while waiting
443 * for it to empty or we may deadlock with another cpu. Create a CPU_*()
444 * function to do this! YYY we really should 'block' here.
446 * MUST be called from a critical section. This routine may be called
447 * from an interrupt (for example, if an interrupt wakes a foreign thread
451 lwkt_wait_ipiq(globaldata_t target, int seq)
455 if (target != mycpu) {
456 ip = &mycpu->gd_ipiq[target->gd_cpuid];
457 if ((int)(ip->ip_xindex - seq) < 0) {
458 #if defined(__i386__)
459 unsigned int eflags = read_eflags();
460 #elif defined(__x86_64__)
461 unsigned long rflags = read_rflags();
463 int64_t time_tgt = tsc_get_target(1000000000LL);
468 DEBUG_PUSH_INFO("wait_ipiq");
469 while ((int)(ip->ip_xindex - seq) < 0) {
475 * IPIQs must be handled within 10 seconds and this code
476 * will warn after one second.
478 if ((benice & 255) == 0 && tsc_test_target(time_tgt) > 0) {
479 kprintf("LWKT_WAIT_IPIQ WARNING! %d wait %d (%d)\n",
480 mycpu->gd_cpuid, target->gd_cpuid,
481 ip->ip_xindex - seq);
482 if (--time_loops == 0)
483 panic("LWKT_WAIT_IPIQ");
484 time_tgt = tsc_get_target(1000000000LL);
489 * xindex may be modified by another cpu, use a load fence
490 * to ensure that the loop does not use a speculative value
491 * (which may improve performance).
496 #if defined(__i386__)
497 write_eflags(eflags);
498 #elif defined(__x86_64__)
499 write_rflags(rflags);
506 lwkt_seq_ipiq(globaldata_t target)
510 ip = &mycpu->gd_ipiq[target->gd_cpuid];
511 return(ip->ip_windex);
515 * Called from IPI interrupt (like a fast interrupt), which has placed
516 * us in a critical section. The MP lock may or may not be held.
517 * May also be called from doreti or splz, or be reentrantly called
518 * indirectly through the ip_info[].func we run.
520 * There are two versions, one where no interrupt frame is available (when
521 * called from the send code and from splz, and one where an interrupt
522 * frame is available.
524 * When the current cpu is mastering a cpusync we do NOT internally loop
525 * on the cpusyncq poll. We also do not re-flag a pending ipi due to
526 * the cpusyncq poll because this can cause doreti/splz to loop internally.
527 * The cpusync master's own loop must be allowed to run to avoid a deadlock.
530 lwkt_process_ipiq(void)
532 globaldata_t gd = mycpu;
538 ++gd->gd_processing_ipiq;
541 mask = gd->gd_ipimask;
542 atomic_clear_cpumask(&gd->gd_ipimask, mask);
544 n = BSFCPUMASK(mask);
545 if (n != gd->gd_cpuid) {
546 sgd = globaldata_find(n);
549 while (lwkt_process_ipiq_core(sgd, &ip[gd->gd_cpuid], NULL))
557 * Process pending cpusyncs. If the current thread has a cpusync
558 * active cpusync we only run the list once and do not re-flag
559 * as the thread itself is processing its interlock.
561 if (lwkt_process_ipiq_core(gd, &gd->gd_cpusyncq, NULL)) {
562 if (gd->gd_curthread->td_cscount == 0)
564 /* need_ipiq(); do not reflag */
568 * Interlock to allow more IPI interrupts. Recheck ipimask after
569 * releasing gd_npoll.
573 atomic_poll_release_int(&gd->gd_npoll);
577 --gd->gd_processing_ipiq;
581 lwkt_process_ipiq_frame(struct intrframe *frame)
583 globaldata_t gd = mycpu;
591 mask = gd->gd_ipimask;
592 atomic_clear_cpumask(&gd->gd_ipimask, mask);
594 n = BSFCPUMASK(mask);
595 if (n != gd->gd_cpuid) {
596 sgd = globaldata_find(n);
599 while (lwkt_process_ipiq_core(sgd, &ip[gd->gd_cpuid], frame))
605 if (gd->gd_cpusyncq.ip_rindex != gd->gd_cpusyncq.ip_windex) {
606 if (lwkt_process_ipiq_core(gd, &gd->gd_cpusyncq, frame)) {
607 if (gd->gd_curthread->td_cscount == 0)
609 /* need_ipiq(); do not reflag */
614 * Interlock to allow more IPI interrupts. Recheck ipimask after
615 * releasing gd_npoll.
619 atomic_poll_release_int(&gd->gd_npoll);
626 static int iqticks[SMP_MAXCPU];
627 static int iqcount[SMP_MAXCPU];
630 static int iqterm[SMP_MAXCPU];
634 lwkt_process_ipiq_core(globaldata_t sgd, lwkt_ipiq_t ip,
635 struct intrframe *frame)
637 globaldata_t mygd = mycpu;
640 ipifunc3_t copy_func;
645 if (iqticks[mygd->gd_cpuid] != ticks) {
646 iqticks[mygd->gd_cpuid] = ticks;
647 iqcount[mygd->gd_cpuid] = 0;
649 if (++iqcount[mygd->gd_cpuid] > 3000000) {
650 kprintf("cpu %d ipiq maxed cscount %d spin %d\n",
652 mygd->gd_curthread->td_cscount,
654 iqcount[mygd->gd_cpuid] = 0;
656 if (++iqterm[mygd->gd_cpuid] > 10)
657 panic("cpu %d ipiq maxed", mygd->gd_cpuid);
660 for (i = 0; i < ncpus; ++i) {
661 if (globaldata_find(i)->gd_infomsg)
662 kprintf(" %s", globaldata_find(i)->gd_infomsg);
669 * Clear the originating core from our ipimask, we will process all
672 * Obtain the current write index, which is modified by a remote cpu.
673 * Issue a load fence to prevent speculative reads of e.g. data written
674 * by the other cpu prior to it updating the index.
676 KKASSERT(curthread->td_critcount);
679 ++mygd->gd_intr_nesting_level;
682 * NOTE: xindex is only updated after we are sure the function has
683 * finished execution. Beware lwkt_process_ipiq() reentrancy!
684 * The function may send an IPI which may block/drain.
686 * NOTE: Due to additional IPI operations that the callback function
687 * may make, it is possible for both rindex and windex to advance and
688 * thus for rindex to advance passed our cached windex.
690 * NOTE: A load fence is required to prevent speculative loads prior
691 * to the loading of ip_rindex. Even though stores might be
692 * ordered, loads are probably not. A memory fence is required
693 * to prevent reordering of the loads after the ip_rindex update.
695 * NOTE: Single pass only. Returns non-zero if the queue is not empty
698 while (wi - (ri = ip->ip_rindex) > 0) {
699 ri &= MAXCPUFIFO_MASK;
701 copy_func = ip->ip_info[ri].func;
702 copy_arg1 = ip->ip_info[ri].arg1;
703 copy_arg2 = ip->ip_info[ri].arg2;
706 KKASSERT((ip->ip_rindex & MAXCPUFIFO_MASK) ==
707 ((ri + 1) & MAXCPUFIFO_MASK));
708 logipiq(receive, copy_func, copy_arg1, copy_arg2, sgd, mycpu);
710 if (ipiq_debug && (ip->ip_rindex & 0xFFFFFF) == 0) {
711 kprintf("cpu %d ipifunc %p %p %d (frame %p)\n",
713 copy_func, copy_arg1, copy_arg2,
714 #if defined(__i386__)
715 (frame ? (void *)frame->if_eip : NULL));
716 #elif defined(__x86_64__)
717 (frame ? (void *)frame->if_rip : NULL));
723 copy_func(copy_arg1, copy_arg2, frame);
725 ip->ip_xindex = ip->ip_rindex;
729 * Simulate panics during the processing of an IPI
731 if (mycpu->gd_cpuid == panic_ipiq_cpu && panic_ipiq_count) {
732 if (--panic_ipiq_count == 0) {
734 Debugger("PANIC_DEBUG");
736 panic("PANIC_DEBUG");
742 --mygd->gd_intr_nesting_level;
745 * Return non-zero if there is still more in the queue.
748 return (ip->ip_rindex != ip->ip_windex);
752 lwkt_sync_ipiq(void *arg)
754 volatile cpumask_t *cpumask = arg;
756 atomic_clear_cpumask(cpumask, mycpu->gd_cpumask);
762 lwkt_synchronize_ipiqs(const char *wmesg)
764 volatile cpumask_t other_cpumask;
766 other_cpumask = mycpu->gd_other_cpus & smp_active_mask;
767 lwkt_send_ipiq_mask(other_cpumask, lwkt_sync_ipiq,
768 __DEVOLATILE(void *, &other_cpumask));
770 while (other_cpumask != 0) {
771 tsleep_interlock(&other_cpumask, 0);
772 if (other_cpumask != 0)
773 tsleep(&other_cpumask, PINTERLOCKED, wmesg, 0);
778 * CPU Synchronization Support
780 * lwkt_cpusync_interlock() - Place specified cpus in a quiescent state.
781 * The current cpu is placed in a hard critical
784 * lwkt_cpusync_deinterlock() - Execute cs_func on specified cpus, including
785 * current cpu if specified, then return.
788 lwkt_cpusync_simple(cpumask_t mask, cpusync_func_t func, void *arg)
790 struct lwkt_cpusync cs;
792 lwkt_cpusync_init(&cs, mask, func, arg);
793 lwkt_cpusync_interlock(&cs);
794 lwkt_cpusync_deinterlock(&cs);
799 lwkt_cpusync_interlock(lwkt_cpusync_t cs)
802 const char *smsg = "SMPSYNL";
804 globaldata_t gd = mycpu;
808 * mask acknowledge (cs_mack): 0->mask for stage 1
810 * mack does not include the current cpu.
812 mask = cs->cs_mask & gd->gd_other_cpus & smp_active_mask;
814 crit_enter_id("cpusync");
816 DEBUG_PUSH_INFO("cpusync_interlock");
817 ++ipiq_stat(gd).ipiq_cscount;
818 ++gd->gd_curthread->td_cscount;
819 lwkt_send_ipiq_mask(mask, (ipifunc1_t)lwkt_cpusync_remote1, cs);
820 logipiq2(sync_start, (long)mask);
822 if (gd->gd_curthread->td_wmesg == NULL)
823 gd->gd_curthread->td_wmesg = smsg;
825 while (cs->cs_mack != mask) {
828 #ifdef _KERNEL_VIRTUAL
833 if (gd->gd_curthread->td_wmesg == smsg)
834 gd->gd_curthread->td_wmesg = NULL;
841 * Interlocked cpus have executed remote1 and are polling in remote2.
842 * To deinterlock we clear cs_mack and wait for the cpus to execute
843 * the func and set their bit in cs_mack again.
847 lwkt_cpusync_deinterlock(lwkt_cpusync_t cs)
849 globaldata_t gd = mycpu;
851 const char *smsg = "SMPSYNU";
856 * mask acknowledge (cs_mack): mack->0->mack for stage 2
858 * Clearing cpu bits for polling cpus in cs_mack will cause them to
859 * execute stage 2, which executes the cs_func(cs_data) and then sets
860 * their bit in cs_mack again.
862 * mack does not include the current cpu.
868 if (cs->cs_func && (cs->cs_mask & gd->gd_cpumask))
869 cs->cs_func(cs->cs_data);
871 DEBUG_PUSH_INFO("cpusync_deinterlock");
873 if (gd->gd_curthread->td_wmesg == NULL)
874 gd->gd_curthread->td_wmesg = smsg;
876 while (cs->cs_mack != mask) {
879 #ifdef _KERNEL_VIRTUAL
884 if (gd->gd_curthread->td_wmesg == smsg)
885 gd->gd_curthread->td_wmesg = NULL;
889 * cpusyncq ipis may be left queued without the RQF flag set due to
890 * a non-zero td_cscount, so be sure to process any laggards after
891 * decrementing td_cscount.
893 --gd->gd_curthread->td_cscount;
895 logipiq2(sync_end, (long)mask);
897 crit_exit_id("cpusync");
901 * helper IPI remote messaging function.
903 * Called on remote cpu when a new cpu synchronization request has been
904 * sent to us. Execute the run function and adjust cs_count, then requeue
905 * the request so we spin on it.
908 lwkt_cpusync_remote1(lwkt_cpusync_t cs)
910 globaldata_t gd = mycpu;
912 atomic_set_cpumask(&cs->cs_mack, gd->gd_cpumask);
913 lwkt_cpusync_remote2(cs);
917 * helper IPI remote messaging function.
919 * Poll for the originator telling us to finish. If it hasn't, requeue
920 * our request so we spin on it.
923 lwkt_cpusync_remote2(lwkt_cpusync_t cs)
925 globaldata_t gd = mycpu;
927 if ((cs->cs_mack & gd->gd_cpumask) == 0) {
929 cs->cs_func(cs->cs_data);
930 atomic_set_cpumask(&cs->cs_mack, gd->gd_cpumask);
931 /* cs can be ripped out at this point */
936 #ifdef _KERNEL_VIRTUAL
939 ip = &gd->gd_cpusyncq;
940 wi = ip->ip_windex & MAXCPUFIFO_MASK;
941 ip->ip_info[wi].func = (ipifunc3_t)(ipifunc1_t)lwkt_cpusync_remote2;
942 ip->ip_info[wi].arg1 = cs;
943 ip->ip_info[wi].arg2 = 0;
945 KKASSERT(ip->ip_windex - ip->ip_rindex < MAXCPUFIFO);
947 if (ipiq_debug && (ip->ip_windex & 0xFFFFFF) == 0) {
948 kprintf("cpu %d cm=%016jx %016jx f=%p\n",
950 (intmax_t)cs->cs_mask, (intmax_t)cs->cs_mack,