2 * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $DragonFly: src/sys/kern/lwkt_ipiq.c,v 1.4 2004/03/01 06:33:17 dillon Exp $
30 * This module implements IPI message queueing and the MI portion of IPI
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/rtprio.h>
41 #include <sys/queue.h>
42 #include <sys/thread2.h>
43 #include <sys/sysctl.h>
44 #include <sys/kthread.h>
45 #include <machine/cpu.h>
50 #include <vm/vm_param.h>
51 #include <vm/vm_kern.h>
52 #include <vm/vm_object.h>
53 #include <vm/vm_page.h>
54 #include <vm/vm_map.h>
55 #include <vm/vm_pager.h>
56 #include <vm/vm_extern.h>
57 #include <vm/vm_zone.h>
59 #include <machine/stdarg.h>
60 #include <machine/ipl.h>
61 #include <machine/smp.h>
62 #include <machine/atomic.h>
64 #define THREAD_STACK (UPAGES * PAGE_SIZE)
68 #include <sys/stdint.h>
69 #include <libcaps/thread.h>
70 #include <sys/thread.h>
71 #include <sys/msgport.h>
72 #include <sys/errno.h>
73 #include <libcaps/globaldata.h>
74 #include <sys/thread2.h>
75 #include <sys/msgport2.h>
79 #include <machine/cpufunc.h>
80 #include <machine/lock.h>
85 static __int64_t ipiq_count;
86 static __int64_t ipiq_fifofull;
87 static __int64_t ipiq_cscount;
93 SYSCTL_QUAD(_lwkt, OID_AUTO, ipiq_count, CTLFLAG_RW, &ipiq_count, 0, "");
94 SYSCTL_QUAD(_lwkt, OID_AUTO, ipiq_fifofull, CTLFLAG_RW, &ipiq_fifofull, 0, "");
95 SYSCTL_QUAD(_lwkt, OID_AUTO, ipiq_cscount, CTLFLAG_RW, &ipiq_cscount, 0, "");
102 static int lwkt_process_ipiq1(lwkt_ipiq_t ip, struct intrframe *frame);
103 static void lwkt_cpusync_remote1(lwkt_cpusync_t poll);
104 static void lwkt_cpusync_remote2(lwkt_cpusync_t poll);
107 * Send a function execution request to another cpu. The request is queued
108 * on the cpu<->cpu ipiq matrix. Each cpu owns a unique ipiq FIFO for every
109 * possible target cpu. The FIFO can be written.
111 * YYY If the FIFO fills up we have to enable interrupts and process the
112 * IPIQ while waiting for it to empty or we may deadlock with another cpu.
113 * Create a CPU_*() function to do this!
115 * We can safely bump gd_intr_nesting_level because our crit_exit() at the
116 * end will take care of any pending interrupts.
118 * Must be called from a critical section.
121 lwkt_send_ipiq(globaldata_t target, ipifunc_t func, void *arg)
125 struct globaldata *gd = mycpu;
132 ++gd->gd_intr_nesting_level;
134 if (gd->gd_intr_nesting_level > 20)
135 panic("lwkt_send_ipiq: TOO HEAVILY NESTED!");
137 KKASSERT(curthread->td_pri >= TDPRI_CRIT);
139 ip = &gd->gd_ipiq[target->gd_cpuid];
142 * We always drain before the FIFO becomes full so it should never
143 * become full. We need to leave enough entries to deal with
146 KKASSERT(ip->ip_windex - ip->ip_rindex != MAXCPUFIFO);
147 windex = ip->ip_windex & MAXCPUFIFO_MASK;
148 ip->ip_func[windex] = (ipifunc2_t)func;
149 ip->ip_arg[windex] = arg;
152 if (ip->ip_windex - ip->ip_rindex > MAXCPUFIFO / 2) {
153 unsigned int eflags = read_eflags();
156 while (ip->ip_windex - ip->ip_rindex > MAXCPUFIFO / 4) {
157 KKASSERT(ip->ip_windex - ip->ip_rindex != MAXCPUFIFO - 1);
160 write_eflags(eflags);
162 --gd->gd_intr_nesting_level;
163 cpu_send_ipiq(target->gd_cpuid); /* issues mem barrier if appropriate */
165 return(ip->ip_windex);
169 * Send an IPI request passively, return 0 on success and ENOENT on failure.
170 * This routine does not recursive through lwkt_process_ipiq() nor does it
171 * block trying to queue the actual IPI. If we successfully queue the
172 * message but fail to queue the IPI, we still count it as a success.
173 * The occassional small race against a target cpu HLT is recovered at
174 * the next clock interrupt.
177 lwkt_send_ipiq_passive(globaldata_t target, ipifunc_t func, void *arg)
181 struct globaldata *gd = mycpu;
183 KKASSERT(curthread->td_pri >= TDPRI_CRIT);
189 ip = &gd->gd_ipiq[target->gd_cpuid];
191 if (ip->ip_windex - ip->ip_rindex >= MAXCPUFIFO - 1) {
194 windex = ip->ip_windex & MAXCPUFIFO_MASK;
195 ip->ip_func[windex] = (ipifunc2_t)func;
196 ip->ip_arg[windex] = arg;
200 * passive mode doesn't work yet :-(
203 cpu_send_ipiq(target->gd_cpuid);
205 cpu_send_ipiq_passive(target->gd_cpuid);
211 * deprecated, used only by fast int forwarding.
214 lwkt_send_ipiq_bycpu(int dcpu, ipifunc_t func, void *arg)
216 return(lwkt_send_ipiq(globaldata_find(dcpu), func, arg));
220 * Send a message to several target cpus. Typically used for scheduling.
221 * The message will not be sent to stopped cpus.
224 lwkt_send_ipiq_mask(u_int32_t mask, ipifunc_t func, void *arg)
229 mask &= ~stopped_cpus;
232 lwkt_send_ipiq(globaldata_find(cpuid), func, arg);
233 mask &= ~(1 << cpuid);
240 * Wait for the remote cpu to finish processing a function.
242 * YYY we have to enable interrupts and process the IPIQ while waiting
243 * for it to empty or we may deadlock with another cpu. Create a CPU_*()
244 * function to do this! YYY we really should 'block' here.
246 * MUST be called from a critical section. This routine may be called
247 * from an interrupt (for example, if an interrupt wakes a foreign thread
251 lwkt_wait_ipiq(globaldata_t target, int seq)
254 int maxc = 100000000;
256 if (target != mycpu) {
257 ip = &mycpu->gd_ipiq[target->gd_cpuid];
258 if ((int)(ip->ip_xindex - seq) < 0) {
259 unsigned int eflags = read_eflags();
261 while ((int)(ip->ip_xindex - seq) < 0) {
266 printf("LWKT_WAIT_IPIQ WARNING! %d wait %d (%d)\n", mycpu->gd_cpuid, target->gd_cpuid, ip->ip_xindex - seq);
268 panic("LWKT_WAIT_IPIQ");
270 write_eflags(eflags);
276 lwkt_seq_ipiq(globaldata_t target)
280 ip = &mycpu->gd_ipiq[target->gd_cpuid];
281 return(ip->ip_windex);
285 * Called from IPI interrupt (like a fast interrupt), which has placed
286 * us in a critical section. The MP lock may or may not be held.
287 * May also be called from doreti or splz, or be reentrantly called
288 * indirectly through the ip_func[] we run.
290 * There are two versions, one where no interrupt frame is available (when
291 * called from the send code and from splz, and one where an interrupt
292 * frame is available.
295 lwkt_process_ipiq(void)
297 globaldata_t gd = mycpu;
302 for (n = 0; n < ncpus; ++n) {
303 if (n != gd->gd_cpuid) {
304 ip = globaldata_find(n)->gd_ipiq;
306 while (lwkt_process_ipiq1(&ip[gd->gd_cpuid], NULL))
311 if (gd->gd_cpusyncq.ip_rindex != gd->gd_cpusyncq.ip_windex) {
312 if (lwkt_process_ipiq1(&gd->gd_cpusyncq, NULL)) {
313 if (gd->gd_curthread->td_cscount == 0)
322 lwkt_process_ipiq_frame(struct intrframe frame)
324 globaldata_t gd = mycpu;
329 for (n = 0; n < ncpus; ++n) {
330 if (n != gd->gd_cpuid) {
331 ip = globaldata_find(n)->gd_ipiq;
333 while (lwkt_process_ipiq1(&ip[gd->gd_cpuid], &frame))
338 if (gd->gd_cpusyncq.ip_rindex != gd->gd_cpusyncq.ip_windex) {
339 if (lwkt_process_ipiq1(&gd->gd_cpusyncq, &frame)) {
340 if (gd->gd_curthread->td_cscount == 0)
349 lwkt_process_ipiq1(lwkt_ipiq_t ip, struct intrframe *frame)
352 int wi = ip->ip_windex;
354 * Note: xindex is only updated after we are sure the function has
355 * finished execution. Beware lwkt_process_ipiq() reentrancy! The
356 * function may send an IPI which may block/drain.
358 while ((ri = ip->ip_rindex) != wi) {
359 ip->ip_rindex = ri + 1;
360 ri &= MAXCPUFIFO_MASK;
361 ip->ip_func[ri](ip->ip_arg[ri], frame);
362 /* YYY memory barrier */
363 ip->ip_xindex = ip->ip_rindex;
365 return(wi != ip->ip_windex);
371 * !SMP dummy routines
375 lwkt_send_ipiq(globaldata_t target, ipifunc_t func, void *arg)
377 panic("lwkt_send_ipiq: UP box! (%d,%p,%p)", target->gd_cpuid, func, arg);
378 return(0); /* NOT REACHED */
382 lwkt_wait_ipiq(globaldata_t target, int seq)
384 panic("lwkt_wait_ipiq: UP box! (%d,%d)", target->gd_cpuid, seq);
390 * CPU Synchronization Support
392 * lwkt_cpusync_simple()
394 * The function is executed synchronously before return on remote cpus.
395 * A lwkt_cpusync_t pointer is passed as an argument. The data can
396 * be accessed via arg->cs_data.
398 * XXX should I just pass the data as an argument to be consistent?
402 lwkt_cpusync_simple(cpumask_t mask, cpusync_func_t func, void *data)
404 struct lwkt_cpusync cmd;
406 cmd.cs_run_func = NULL;
407 cmd.cs_fin1_func = func;
408 cmd.cs_fin2_func = NULL;
410 lwkt_cpusync_start(mask & mycpu->gd_other_cpus, &cmd);
411 if (mask & (1 << mycpu->gd_cpuid))
413 lwkt_cpusync_finish(&cmd);
417 * lwkt_cpusync_fastdata()
419 * The function is executed in tandem with return on remote cpus.
420 * The data is directly passed as an argument. Do not pass pointers to
421 * temporary storage as the storage might have
422 * gone poof by the time the target cpu executes
425 * At the moment lwkt_cpusync is declared on the stack and we must wait
426 * for all remote cpus to ack in lwkt_cpusync_finish(), but as a future
427 * optimization we should be able to put a counter in the globaldata
428 * structure (if it is not otherwise being used) and just poke it and
429 * return without waiting. XXX
432 lwkt_cpusync_fastdata(cpumask_t mask, cpusync_func2_t func, void *data)
434 struct lwkt_cpusync cmd;
436 cmd.cs_run_func = NULL;
437 cmd.cs_fin1_func = NULL;
438 cmd.cs_fin2_func = func;
440 lwkt_cpusync_start(mask & mycpu->gd_other_cpus, &cmd);
441 if (mask & (1 << mycpu->gd_cpuid))
443 lwkt_cpusync_finish(&cmd);
447 * lwkt_cpusync_start()
449 * Start synchronization with a set of target cpus, return once they are
450 * known to be in a synchronization loop. The target cpus will execute
451 * poll->cs_run_func() IN TANDEM WITH THE RETURN.
453 * XXX future: add lwkt_cpusync_start_quick() and require a call to
454 * lwkt_cpusync_add() or lwkt_cpusync_wait(), allowing the caller to
455 * potentially absorb the IPI latency doing something useful.
458 lwkt_cpusync_start(cpumask_t mask, lwkt_cpusync_t poll)
460 globaldata_t gd = mycpu;
463 poll->cs_mask = mask;
465 poll->cs_maxcount = lwkt_send_ipiq_mask(
466 mask & gd->gd_other_cpus & smp_active_mask,
467 (ipifunc_t)lwkt_cpusync_remote1, poll);
469 if (mask & (1 << gd->gd_cpuid)) {
470 if (poll->cs_run_func)
471 poll->cs_run_func(poll);
474 if (poll->cs_maxcount) {
476 ++gd->gd_curthread->td_cscount;
477 while (poll->cs_count != poll->cs_maxcount) {
487 lwkt_cpusync_add(cpumask_t mask, lwkt_cpusync_t poll)
489 globaldata_t gd = mycpu;
494 mask &= ~poll->cs_mask;
495 poll->cs_mask |= mask;
497 count = lwkt_send_ipiq_mask(
498 mask & gd->gd_other_cpus & smp_active_mask,
499 (ipifunc_t)lwkt_cpusync_remote1, poll);
501 if (mask & (1 << gd->gd_cpuid)) {
502 if (poll->cs_run_func)
503 poll->cs_run_func(poll);
506 poll->cs_maxcount += count;
507 if (poll->cs_maxcount) {
508 if (poll->cs_maxcount == count)
509 ++gd->gd_curthread->td_cscount;
510 while (poll->cs_count != poll->cs_maxcount) {
520 * Finish synchronization with a set of target cpus. The target cpus will
521 * execute cs_fin1_func(poll) prior to this function returning, and will
522 * execute cs_fin2_func(data) IN TANDEM WITH THIS FUNCTION'S RETURN.
524 * If cs_maxcount is non-zero then we are mastering a cpusync with one or
525 * more remote cpus and must account for it in our thread structure.
528 lwkt_cpusync_finish(lwkt_cpusync_t poll)
530 globaldata_t gd = mycpu;
533 if (poll->cs_mask & (1 << gd->gd_cpuid)) {
534 if (poll->cs_fin1_func)
535 poll->cs_fin1_func(poll);
536 if (poll->cs_fin2_func)
537 poll->cs_fin2_func(poll->cs_data);
540 if (poll->cs_maxcount) {
541 while (poll->cs_count != -(poll->cs_maxcount + 1)) {
546 --gd->gd_curthread->td_cscount;
554 * helper IPI remote messaging function.
556 * Called on remote cpu when a new cpu synchronization request has been
557 * sent to us. Execute the run function and adjust cs_count, then requeue
558 * the request so we spin on it.
561 lwkt_cpusync_remote1(lwkt_cpusync_t poll)
563 atomic_add_int(&poll->cs_count, 1);
564 if (poll->cs_run_func)
565 poll->cs_run_func(poll);
566 lwkt_cpusync_remote2(poll);
570 * helper IPI remote messaging function.
572 * Poll for the originator telling us to finish. If it hasn't, requeue
573 * our request so we spin on it. When the originator requests that we
574 * finish we execute cs_fin1_func(poll) synchronously and cs_fin2_func(data)
575 * in tandem with the release.
578 lwkt_cpusync_remote2(lwkt_cpusync_t poll)
580 if (poll->cs_count < 0) {
581 cpusync_func2_t savef;
584 if (poll->cs_fin1_func)
585 poll->cs_fin1_func(poll);
586 if (poll->cs_fin2_func) {
587 savef = poll->cs_fin2_func;
588 saved = poll->cs_data;
589 atomic_add_int(&poll->cs_count, -1);
592 atomic_add_int(&poll->cs_count, -1);
595 globaldata_t gd = mycpu;
599 ip = &gd->gd_cpusyncq;
600 wi = ip->ip_windex & MAXCPUFIFO_MASK;
601 ip->ip_func[wi] = (ipifunc2_t)lwkt_cpusync_remote2;
602 ip->ip_arg[wi] = poll;