xref: /netbsd-src/sys/arch/xen/x86/xen_ipi.c (revision 781d596f33ed788d7129e3166eecd1a64814a75e)
1*781d596fSrin /* $NetBSD: xen_ipi.c,v 1.42 2023/11/06 17:01:07 rin Exp $ */
2d7b11fa4Scherry 
3d7b11fa4Scherry /*-
4115c1bc0Sad  * Copyright (c) 2011, 2019 The NetBSD Foundation, Inc.
5d7b11fa4Scherry  * All rights reserved.
6d7b11fa4Scherry  *
7d7b11fa4Scherry  * This code is derived from software contributed to The NetBSD Foundation
8d7b11fa4Scherry  * by Cherry G. Mathew <cherry@zyx.in>
9d7b11fa4Scherry  *
10d7b11fa4Scherry  * Redistribution and use in source and binary forms, with or without
11d7b11fa4Scherry  * modification, are permitted provided that the following conditions
12d7b11fa4Scherry  * are met:
13d7b11fa4Scherry  * 1. Redistributions of source code must retain the above copyright
14d7b11fa4Scherry  *    notice, this list of conditions and the following disclaimer.
15d7b11fa4Scherry  * 2. Redistributions in binary form must reproduce the above copyright
16d7b11fa4Scherry  *    notice, this list of conditions and the following disclaimer in the
17d7b11fa4Scherry  *    documentation and/or other materials provided with the distribution.
18d7b11fa4Scherry  *
19d7b11fa4Scherry  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20d7b11fa4Scherry  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21d7b11fa4Scherry  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22d7b11fa4Scherry  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23d7b11fa4Scherry  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24d7b11fa4Scherry  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25d7b11fa4Scherry  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26d7b11fa4Scherry  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27d7b11fa4Scherry  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28d7b11fa4Scherry  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29d7b11fa4Scherry  * POSSIBILITY OF SUCH DAMAGE.
30d7b11fa4Scherry  */
31d7b11fa4Scherry 
32d7b11fa4Scherry #include <sys/cdefs.h>			/* RCS ID macro */
33d7b11fa4Scherry 
34d7b11fa4Scherry /*
35d7b11fa4Scherry  * Based on: x86/ipi.c
36d7b11fa4Scherry  */
37d7b11fa4Scherry 
38*781d596fSrin __KERNEL_RCSID(0, "$NetBSD: xen_ipi.c,v 1.42 2023/11/06 17:01:07 rin Exp $");
395ebfdbc0Sjdolecek 
405ebfdbc0Sjdolecek #include "opt_ddb.h"
41d7b11fa4Scherry 
42d7b11fa4Scherry #include <sys/types.h>
43d7b11fa4Scherry 
44d7b11fa4Scherry #include <sys/atomic.h>
45d7b11fa4Scherry #include <sys/cpu.h>
46d3ccea85Sbouyer #include <sys/mutex.h>
47d7b11fa4Scherry #include <sys/device.h>
48d7b11fa4Scherry #include <sys/xcall.h>
498011b285Srmind #include <sys/ipi.h>
50d7b11fa4Scherry #include <sys/errno.h>
51d7b11fa4Scherry #include <sys/systm.h>
52d7b11fa4Scherry 
53742d777cSdsl #include <x86/fpu.h>
54d7b11fa4Scherry #include <machine/frame.h>
55d7b11fa4Scherry #include <machine/segments.h>
56d7b11fa4Scherry 
57e0ec0318Scherry #include <xen/evtchn.h>
58d7b11fa4Scherry #include <xen/intr.h>
59d7b11fa4Scherry #include <xen/intrdefs.h>
60d7b11fa4Scherry #include <xen/hypervisor.h>
61ac8432e2Scherry #include <xen/include/public/vcpu.h>
62d7b11fa4Scherry 
635ebfdbc0Sjdolecek #ifdef DDB
64d7b11fa4Scherry extern void ddb_ipi(struct trapframe);
655ebfdbc0Sjdolecek static void xen_ipi_ddb(struct cpu_info *, struct intrframe *);
665ebfdbc0Sjdolecek #endif
67d7b11fa4Scherry 
68d7b11fa4Scherry static void xen_ipi_halt(struct cpu_info *, struct intrframe *);
69f0ceb578Schristos static void xen_ipi_synch_fpu(struct cpu_info *, struct intrframe *);
70d7b11fa4Scherry static void xen_ipi_xcall(struct cpu_info *, struct intrframe *);
71926a9338Scherry static void xen_ipi_hvcb(struct cpu_info *, struct intrframe *);
728011b285Srmind static void xen_ipi_generic(struct cpu_info *, struct intrframe *);
73115c1bc0Sad static void xen_ipi_ast(struct cpu_info *, struct intrframe *);
74c24c993fSbouyer static void xen_ipi_kpreempt(struct cpu_info *ci, struct intrframe *);
75d7b11fa4Scherry 
76c24c993fSbouyer static void (*xen_ipifunc[XEN_NIPIS])(struct cpu_info *, struct intrframe *) =
77d7b11fa4Scherry {	/* In order of priority (see: xen/include/intrdefs.h */
78d7b11fa4Scherry 	xen_ipi_halt,
79f0ceb578Schristos 	xen_ipi_synch_fpu,
805ebfdbc0Sjdolecek #ifdef DDB
81d7b11fa4Scherry 	xen_ipi_ddb,
825ebfdbc0Sjdolecek #else
835ebfdbc0Sjdolecek 	NULL,
845ebfdbc0Sjdolecek #endif
85926a9338Scherry 	xen_ipi_xcall,
868011b285Srmind 	xen_ipi_hvcb,
878011b285Srmind 	xen_ipi_generic,
88c24c993fSbouyer 	xen_ipi_ast,
89c24c993fSbouyer 	xen_ipi_kpreempt
90d7b11fa4Scherry };
91d7b11fa4Scherry 
92e5c2d6aaScherry static int
xen_ipi_handler(void * arg,struct intrframe * regs)932095a152Sriastradh xen_ipi_handler(void *arg, struct intrframe *regs)
94d7b11fa4Scherry {
95d7b11fa4Scherry 	uint32_t pending;
96d7b11fa4Scherry 	int bit;
97e5c2d6aaScherry 	struct cpu_info *ci;
98e5c2d6aaScherry 
99e5c2d6aaScherry 	ci = curcpu();
100d7b11fa4Scherry 
101a27ce3b1Sbouyer 	KASSERT(ci == arg);
102d7b11fa4Scherry 	pending = atomic_swap_32(&ci->ci_ipis, 0);
103d7b11fa4Scherry 
104d7b11fa4Scherry 	KDASSERT((pending >> XEN_NIPIS) == 0);
105d7b11fa4Scherry 	while ((bit = ffs(pending)) != 0) {
106d7b11fa4Scherry 		bit--;
107d7b11fa4Scherry 		pending &= ~(1 << bit);
108d7b11fa4Scherry 		ci->ci_ipi_events[bit].ev_count++;
109c24c993fSbouyer 		if (xen_ipifunc[bit] != NULL) {
110c24c993fSbouyer 			(*xen_ipifunc[bit])(ci, regs);
111941e03e9Scherry 		} else {
112c24c993fSbouyer 			panic("xen_ipifunc[%d] unsupported!\n", bit);
113d7b11fa4Scherry 			/* NOTREACHED */
114d7b11fa4Scherry 		}
115d7b11fa4Scherry 	}
116e5c2d6aaScherry 
117e5c2d6aaScherry 	return 0;
118d7b11fa4Scherry }
119d7b11fa4Scherry 
120d7b11fa4Scherry /* Must be called once for every cpu that expects to send/recv ipis */
121d7b11fa4Scherry void
xen_ipi_init(void)122d7b11fa4Scherry xen_ipi_init(void)
123d7b11fa4Scherry {
124d7b11fa4Scherry 	cpuid_t vcpu;
125d7b11fa4Scherry 	evtchn_port_t evtchn;
126d7b11fa4Scherry 	struct cpu_info *ci;
127f216ec15Sjdolecek 	char intr_xname[INTRDEVNAMEBUF];
128d7b11fa4Scherry 
129d7b11fa4Scherry 	ci = curcpu();
130d7b11fa4Scherry 
131c24c993fSbouyer 	vcpu = ci->ci_vcpuid;
132908dafc2Scegger 	KASSERT(vcpu < XEN_LEGACY_MAX_VCPUS);
133d7b11fa4Scherry 
134941e03e9Scherry 	evtchn = bind_vcpu_to_evtch(vcpu);
135941e03e9Scherry 	ci->ci_ipi_evtchn = evtchn;
136d7b11fa4Scherry 
137d7b11fa4Scherry 	KASSERT(evtchn != -1 && evtchn < NR_EVENT_CHANNELS);
138d7b11fa4Scherry 
139f216ec15Sjdolecek 	snprintf(intr_xname, sizeof(intr_xname), "%s ipi",
140f216ec15Sjdolecek 	    device_xname(ci->ci_dev));
141f216ec15Sjdolecek 
1422095a152Sriastradh 	if (event_set_handler(evtchn,
1432095a152Sriastradh 		__FPTRCAST(int (*)(void *), xen_ipi_handler), ci, IPL_HIGH,
1442095a152Sriastradh 		NULL, intr_xname, true, ci) == NULL) {
145e5c2d6aaScherry 		panic("%s: unable to register ipi handler\n", __func__);
146d7b11fa4Scherry 		/* NOTREACHED */
147d7b11fa4Scherry 	}
148d7b11fa4Scherry 
1491fe45bddScherry 	hypervisor_unmask_event(evtchn);
150d7b11fa4Scherry }
151d7b11fa4Scherry 
152*781d596fSrin static inline bool __diagused
valid_ipimask(uint32_t ipimask)153d7b11fa4Scherry valid_ipimask(uint32_t ipimask)
154d7b11fa4Scherry {
1558011b285Srmind 	uint32_t masks = XEN_IPI_GENERIC | XEN_IPI_HVCB | XEN_IPI_XCALL |
156926a9338Scherry 		 XEN_IPI_DDB | XEN_IPI_SYNCH_FPU |
157c24c993fSbouyer 		 XEN_IPI_HALT | XEN_IPI_AST | XEN_IPI_KPREEMPT;
158d7b11fa4Scherry 
159d7b11fa4Scherry 	if (ipimask & ~masks) {
160d7b11fa4Scherry 		return false;
161941e03e9Scherry 	} else {
162d7b11fa4Scherry 		return true;
163d7b11fa4Scherry 	}
164d7b11fa4Scherry 
165d7b11fa4Scherry }
166d7b11fa4Scherry 
167d7b11fa4Scherry int
xen_send_ipi(struct cpu_info * ci,uint32_t ipimask)168d7b11fa4Scherry xen_send_ipi(struct cpu_info *ci, uint32_t ipimask)
169d7b11fa4Scherry {
170d7b11fa4Scherry 	evtchn_port_t evtchn;
171d7b11fa4Scherry 
1727aa684ccSbouyer 	KASSERT(ci != NULL && ci != curcpu());
173d7b11fa4Scherry 
174ce14cd73Scherry 	if ((ci->ci_flags & CPUF_RUNNING) == 0) {
175d7b11fa4Scherry 		return ENOENT;
176d7b11fa4Scherry 	}
177d7b11fa4Scherry 
178d7b11fa4Scherry 	evtchn = ci->ci_ipi_evtchn;
179941e03e9Scherry 
180941e03e9Scherry 	KASSERTMSG(valid_ipimask(ipimask) == true,
181325494feSjym 		"xen_send_ipi() called with invalid ipimask\n");
182d7b11fa4Scherry 
183d7b11fa4Scherry 	atomic_or_32(&ci->ci_ipis, ipimask);
184d7b11fa4Scherry 	hypervisor_notify_via_evtchn(evtchn);
185d7b11fa4Scherry 
186d7b11fa4Scherry 	return 0;
187d7b11fa4Scherry }
188d7b11fa4Scherry 
189d7b11fa4Scherry void
xen_broadcast_ipi(uint32_t ipimask)190d7b11fa4Scherry xen_broadcast_ipi(uint32_t ipimask)
191d7b11fa4Scherry {
192d7b11fa4Scherry 	struct cpu_info *ci, *self = curcpu();
193d7b11fa4Scherry 	CPU_INFO_ITERATOR cii;
194d7b11fa4Scherry 
195941e03e9Scherry 	KASSERTMSG(valid_ipimask(ipimask) == true,
196325494feSjym 		"xen_broadcast_ipi() called with invalid ipimask\n");
197d7b11fa4Scherry 
198d7b11fa4Scherry 	/*
199d7b11fa4Scherry 	 * XXX-cherry: there's an implicit broadcast sending order
200d7b11fa4Scherry 	 * which I dislike. Randomise this ? :-)
201d7b11fa4Scherry 	 */
202d7b11fa4Scherry 
203d7b11fa4Scherry 	for (CPU_INFO_FOREACH(cii, ci)) {
204d7b11fa4Scherry 		if (ci == NULL)
205d7b11fa4Scherry 			continue;
206d7b11fa4Scherry 		if (ci == self)
207d7b11fa4Scherry 			continue;
208d7b11fa4Scherry 		if (ci->ci_data.cpu_idlelwp == NULL)
209d7b11fa4Scherry 			continue;
210d7b11fa4Scherry 		if ((ci->ci_flags & CPUF_PRESENT) == 0)
211d7b11fa4Scherry 			continue;
212d7b11fa4Scherry 		if (ci->ci_flags & (CPUF_RUNNING)) {
213d7b11fa4Scherry 			if (0 != xen_send_ipi(ci, ipimask)) {
214d7b11fa4Scherry 				panic("xen_ipi of %x from %s to %s failed\n",
215d7b11fa4Scherry 				      ipimask, cpu_name(curcpu()),
216d7b11fa4Scherry 				      cpu_name(ci));
217d7b11fa4Scherry 			}
218d7b11fa4Scherry 		}
219d7b11fa4Scherry 	}
220d7b11fa4Scherry }
221d7b11fa4Scherry 
222d7b11fa4Scherry /* MD wrapper for the xcall(9) callback. */
223d7b11fa4Scherry 
224d7b11fa4Scherry static void
xen_ipi_halt(struct cpu_info * ci,struct intrframe * intrf)225d7b11fa4Scherry xen_ipi_halt(struct cpu_info *ci, struct intrframe *intrf)
226d7b11fa4Scherry {
227d7b11fa4Scherry 	KASSERT(ci == curcpu());
228d7b11fa4Scherry 	KASSERT(ci != NULL);
229c24c993fSbouyer 	if (HYPERVISOR_vcpu_op(VCPUOP_down, ci->ci_vcpuid, NULL)) {
230f216ec15Sjdolecek 		panic("%s shutdown failed.\n", device_xname(ci->ci_dev));
231d7b11fa4Scherry 	}
232d7b11fa4Scherry 
233d7b11fa4Scherry }
234d7b11fa4Scherry 
235d7b11fa4Scherry static void
xen_ipi_synch_fpu(struct cpu_info * ci,struct intrframe * intrf)236f0ceb578Schristos xen_ipi_synch_fpu(struct cpu_info *ci, struct intrframe *intrf)
237f0ceb578Schristos {
238f0ceb578Schristos 	KASSERT(ci != NULL);
239f0ceb578Schristos 	KASSERT(intrf != NULL);
240f0ceb578Schristos 
241560337f7Smaxv 	panic("%s: impossible", __func__);
242f0ceb578Schristos }
243f0ceb578Schristos 
2445ebfdbc0Sjdolecek #ifdef DDB
245f0ceb578Schristos static void
xen_ipi_ddb(struct cpu_info * ci,struct intrframe * intrf)246d7b11fa4Scherry xen_ipi_ddb(struct cpu_info *ci, struct intrframe *intrf)
247d7b11fa4Scherry {
248d7b11fa4Scherry 	KASSERT(ci != NULL);
249d7b11fa4Scherry 	KASSERT(intrf != NULL);
250d7b11fa4Scherry 
251d7b11fa4Scherry #ifdef __x86_64__
252d7b11fa4Scherry 	ddb_ipi(intrf->if_tf);
253d7b11fa4Scherry #else
254d7b11fa4Scherry 	struct trapframe tf;
255d7b11fa4Scherry 	tf.tf_gs = intrf->if_gs;
256d7b11fa4Scherry 	tf.tf_fs = intrf->if_fs;
257d7b11fa4Scherry 	tf.tf_es = intrf->if_es;
258d7b11fa4Scherry 	tf.tf_ds = intrf->if_ds;
259d7b11fa4Scherry 	tf.tf_edi = intrf->if_edi;
260d7b11fa4Scherry 	tf.tf_esi = intrf->if_esi;
261d7b11fa4Scherry 	tf.tf_ebp = intrf->if_ebp;
262d7b11fa4Scherry 	tf.tf_ebx = intrf->if_ebx;
263d7b11fa4Scherry 	tf.tf_ecx = intrf->if_ecx;
264d7b11fa4Scherry 	tf.tf_eax = intrf->if_eax;
265d7b11fa4Scherry 	tf.tf_trapno = intrf->__if_trapno;
266d7b11fa4Scherry 	tf.tf_err = intrf->__if_err;
267d7b11fa4Scherry 	tf.tf_eip = intrf->if_eip;
268d7b11fa4Scherry 	tf.tf_cs = intrf->if_cs;
269d7b11fa4Scherry 	tf.tf_eflags = intrf->if_eflags;
270d7b11fa4Scherry 	tf.tf_esp = intrf->if_esp;
271d7b11fa4Scherry 	tf.tf_ss = intrf->if_ss;
272d7b11fa4Scherry 
2732603c867Smaxv 	ddb_ipi(tf);
274d7b11fa4Scherry #endif
275d7b11fa4Scherry }
2765ebfdbc0Sjdolecek #endif /* DDB */
277d7b11fa4Scherry 
278d7b11fa4Scherry static void
xen_ipi_xcall(struct cpu_info * ci,struct intrframe * intrf)279d7b11fa4Scherry xen_ipi_xcall(struct cpu_info *ci, struct intrframe *intrf)
280d7b11fa4Scherry {
281d7b11fa4Scherry 	KASSERT(ci != NULL);
282d7b11fa4Scherry 	KASSERT(intrf != NULL);
283d7b11fa4Scherry 
284d7b11fa4Scherry 	xc_ipi_handler();
285d7b11fa4Scherry }
286d7b11fa4Scherry 
287115c1bc0Sad static void
xen_ipi_ast(struct cpu_info * ci,struct intrframe * intrf)288115c1bc0Sad xen_ipi_ast(struct cpu_info *ci, struct intrframe *intrf)
289115c1bc0Sad {
290115c1bc0Sad 	KASSERT(ci != NULL);
291115c1bc0Sad 	KASSERT(intrf != NULL);
292115c1bc0Sad 
29357eb66c6Sad 	aston(ci->ci_onproc);
294115c1bc0Sad }
295115c1bc0Sad 
296c24c993fSbouyer static void
xen_ipi_generic(struct cpu_info * ci,struct intrframe * intrf)297c24c993fSbouyer xen_ipi_generic(struct cpu_info *ci, struct intrframe *intrf)
298c24c993fSbouyer {
299c24c993fSbouyer 	KASSERT(ci != NULL);
300c24c993fSbouyer 	KASSERT(intrf != NULL);
301c24c993fSbouyer 	ipi_cpu_handler();
302c24c993fSbouyer }
303c24c993fSbouyer 
304c24c993fSbouyer static void
xen_ipi_hvcb(struct cpu_info * ci,struct intrframe * intrf)305c24c993fSbouyer xen_ipi_hvcb(struct cpu_info *ci, struct intrframe *intrf)
306c24c993fSbouyer {
307c24c993fSbouyer 	KASSERT(ci != NULL);
308c24c993fSbouyer 	KASSERT(intrf != NULL);
309c24c993fSbouyer 	KASSERT(ci == curcpu());
310c24c993fSbouyer 	KASSERT(!ci->ci_vcpu->evtchn_upcall_mask);
311c24c993fSbouyer 
312c24c993fSbouyer 	hypervisor_force_callback();
313c24c993fSbouyer }
314c24c993fSbouyer 
315c24c993fSbouyer static void
xen_ipi_kpreempt(struct cpu_info * ci,struct intrframe * intrf)316c24c993fSbouyer xen_ipi_kpreempt(struct cpu_info *ci, struct intrframe * intrf)
317c24c993fSbouyer {
318c24c993fSbouyer 	softint_trigger(1 << SIR_PREEMPT);
319c24c993fSbouyer }
320c24c993fSbouyer 
321c24c993fSbouyer #ifdef XENPV
322d7b11fa4Scherry void
xc_send_ipi(struct cpu_info * ci)323d7b11fa4Scherry xc_send_ipi(struct cpu_info *ci)
324d7b11fa4Scherry {
325d7b11fa4Scherry 
326d7b11fa4Scherry 	KASSERT(kpreempt_disabled());
327d7b11fa4Scherry 	KASSERT(curcpu() != ci);
328d7b11fa4Scherry 	if (ci) {
329d7b11fa4Scherry 		if (0 != xen_send_ipi(ci, XEN_IPI_XCALL)) {
330d7b11fa4Scherry 			panic("xen_send_ipi(XEN_IPI_XCALL) failed\n");
331941e03e9Scherry 		}
332d7b11fa4Scherry 	} else {
333d7b11fa4Scherry 		xen_broadcast_ipi(XEN_IPI_XCALL);
334d7b11fa4Scherry 	}
335d7b11fa4Scherry }
336926a9338Scherry 
3378011b285Srmind void
cpu_ipi(struct cpu_info * ci)3388011b285Srmind cpu_ipi(struct cpu_info *ci)
3398011b285Srmind {
3408011b285Srmind 	KASSERT(kpreempt_disabled());
3418011b285Srmind 	KASSERT(curcpu() != ci);
3428011b285Srmind 	if (ci) {
3438011b285Srmind 		if (0 != xen_send_ipi(ci, XEN_IPI_GENERIC)) {
3448011b285Srmind 			panic("xen_send_ipi(XEN_IPI_GENERIC) failed\n");
3458011b285Srmind 		}
3468011b285Srmind 	} else {
3478011b285Srmind 		xen_broadcast_ipi(XEN_IPI_GENERIC);
3488011b285Srmind 	}
3498011b285Srmind }
350c24c993fSbouyer #endif /* XENPV */
351