xen_ipi.c revision 1.40 1 1.40 christos /* $NetBSD: xen_ipi.c,v 1.40 2022/01/05 20:21:29 christos Exp $ */
2 1.2 cherry
3 1.2 cherry /*-
4 1.34 ad * Copyright (c) 2011, 2019 The NetBSD Foundation, Inc.
5 1.2 cherry * All rights reserved.
6 1.2 cherry *
7 1.2 cherry * This code is derived from software contributed to The NetBSD Foundation
8 1.2 cherry * by Cherry G. Mathew <cherry (at) zyx.in>
9 1.2 cherry *
10 1.2 cherry * Redistribution and use in source and binary forms, with or without
11 1.2 cherry * modification, are permitted provided that the following conditions
12 1.2 cherry * are met:
13 1.2 cherry * 1. Redistributions of source code must retain the above copyright
14 1.2 cherry * notice, this list of conditions and the following disclaimer.
15 1.2 cherry * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 cherry * notice, this list of conditions and the following disclaimer in the
17 1.2 cherry * documentation and/or other materials provided with the distribution.
18 1.2 cherry *
19 1.2 cherry * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2 cherry * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2 cherry * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2 cherry * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2 cherry * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2 cherry * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2 cherry * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2 cherry * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2 cherry * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2 cherry * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2 cherry * POSSIBILITY OF SUCH DAMAGE.
30 1.2 cherry */
31 1.2 cherry
32 1.2 cherry #include <sys/cdefs.h> /* RCS ID macro */
33 1.2 cherry
34 1.2 cherry /*
35 1.2 cherry * Based on: x86/ipi.c
36 1.2 cherry */
37 1.2 cherry
38 1.40 christos __KERNEL_RCSID(0, "$NetBSD: xen_ipi.c,v 1.40 2022/01/05 20:21:29 christos Exp $");
39 1.24 jdolecek
40 1.24 jdolecek #include "opt_ddb.h"
41 1.2 cherry
42 1.2 cherry #include <sys/types.h>
43 1.2 cherry
44 1.2 cherry #include <sys/atomic.h>
45 1.10 bouyer #include <sys/cpu.h>
46 1.2 cherry #include <sys/mutex.h>
47 1.2 cherry #include <sys/device.h>
48 1.2 cherry #include <sys/xcall.h>
49 1.18 rmind #include <sys/ipi.h>
50 1.2 cherry #include <sys/errno.h>
51 1.2 cherry #include <sys/systm.h>
52 1.2 cherry
53 1.16 dsl #include <x86/fpu.h>
54 1.2 cherry #include <machine/frame.h>
55 1.2 cherry #include <machine/segments.h>
56 1.2 cherry
57 1.11 cherry #include <xen/evtchn.h>
58 1.2 cherry #include <xen/intr.h>
59 1.2 cherry #include <xen/intrdefs.h>
60 1.2 cherry #include <xen/hypervisor.h>
61 1.32 cherry #include <xen/include/public/vcpu.h>
62 1.2 cherry
63 1.24 jdolecek #ifdef DDB
64 1.2 cherry extern void ddb_ipi(struct trapframe);
65 1.24 jdolecek static void xen_ipi_ddb(struct cpu_info *, struct intrframe *);
66 1.24 jdolecek #endif
67 1.2 cherry
68 1.2 cherry static void xen_ipi_halt(struct cpu_info *, struct intrframe *);
69 1.14 christos static void xen_ipi_synch_fpu(struct cpu_info *, struct intrframe *);
70 1.2 cherry static void xen_ipi_xcall(struct cpu_info *, struct intrframe *);
71 1.6 cherry static void xen_ipi_hvcb(struct cpu_info *, struct intrframe *);
72 1.18 rmind static void xen_ipi_generic(struct cpu_info *, struct intrframe *);
73 1.34 ad static void xen_ipi_ast(struct cpu_info *, struct intrframe *);
74 1.38 bouyer static void xen_ipi_kpreempt(struct cpu_info *ci, struct intrframe *);
75 1.2 cherry
76 1.38 bouyer static void (*xen_ipifunc[XEN_NIPIS])(struct cpu_info *, struct intrframe *) =
77 1.2 cherry { /* In order of priority (see: xen/include/intrdefs.h */
78 1.2 cherry xen_ipi_halt,
79 1.14 christos xen_ipi_synch_fpu,
80 1.24 jdolecek #ifdef DDB
81 1.2 cherry xen_ipi_ddb,
82 1.24 jdolecek #else
83 1.24 jdolecek NULL,
84 1.24 jdolecek #endif
85 1.6 cherry xen_ipi_xcall,
86 1.18 rmind xen_ipi_hvcb,
87 1.18 rmind xen_ipi_generic,
88 1.38 bouyer xen_ipi_ast,
89 1.38 bouyer xen_ipi_kpreempt
90 1.2 cherry };
91 1.2 cherry
92 1.23 cherry static int
93 1.23 cherry xen_ipi_handler(void *arg)
94 1.2 cherry {
95 1.2 cherry uint32_t pending;
96 1.2 cherry int bit;
97 1.23 cherry struct cpu_info *ci;
98 1.23 cherry struct intrframe *regs;
99 1.2 cherry
100 1.23 cherry ci = curcpu();
101 1.23 cherry regs = arg;
102 1.36 bouyer
103 1.36 bouyer KASSERT(ci == arg);
104 1.23 cherry
105 1.2 cherry pending = atomic_swap_32(&ci->ci_ipis, 0);
106 1.2 cherry
107 1.2 cherry KDASSERT((pending >> XEN_NIPIS) == 0);
108 1.2 cherry while ((bit = ffs(pending)) != 0) {
109 1.2 cherry bit--;
110 1.2 cherry pending &= ~(1 << bit);
111 1.2 cherry ci->ci_ipi_events[bit].ev_count++;
112 1.38 bouyer if (xen_ipifunc[bit] != NULL) {
113 1.38 bouyer (*xen_ipifunc[bit])(ci, regs);
114 1.3 cherry } else {
115 1.38 bouyer panic("xen_ipifunc[%d] unsupported!\n", bit);
116 1.2 cherry /* NOTREACHED */
117 1.2 cherry }
118 1.2 cherry }
119 1.23 cherry
120 1.23 cherry return 0;
121 1.2 cherry }
122 1.2 cherry
123 1.2 cherry /* Must be called once for every cpu that expects to send/recv ipis */
124 1.2 cherry void
125 1.2 cherry xen_ipi_init(void)
126 1.2 cherry {
127 1.2 cherry cpuid_t vcpu;
128 1.2 cherry evtchn_port_t evtchn;
129 1.2 cherry struct cpu_info *ci;
130 1.25 jdolecek char intr_xname[INTRDEVNAMEBUF];
131 1.2 cherry
132 1.2 cherry ci = curcpu();
133 1.2 cherry
134 1.38 bouyer vcpu = ci->ci_vcpuid;
135 1.7 cegger KASSERT(vcpu < XEN_LEGACY_MAX_VCPUS);
136 1.2 cherry
137 1.3 cherry evtchn = bind_vcpu_to_evtch(vcpu);
138 1.3 cherry ci->ci_ipi_evtchn = evtchn;
139 1.2 cherry
140 1.2 cherry KASSERT(evtchn != -1 && evtchn < NR_EVENT_CHANNELS);
141 1.2 cherry
142 1.25 jdolecek snprintf(intr_xname, sizeof(intr_xname), "%s ipi",
143 1.25 jdolecek device_xname(ci->ci_dev));
144 1.25 jdolecek
145 1.36 bouyer if (event_set_handler(evtchn, xen_ipi_handler, ci, IPL_HIGH, NULL,
146 1.39 bouyer intr_xname, true, ci) == NULL) {
147 1.23 cherry panic("%s: unable to register ipi handler\n", __func__);
148 1.2 cherry /* NOTREACHED */
149 1.2 cherry }
150 1.2 cherry
151 1.28 cherry hypervisor_unmask_event(evtchn);
152 1.2 cherry }
153 1.2 cherry
154 1.2 cherry static inline bool /* helper */
155 1.2 cherry valid_ipimask(uint32_t ipimask)
156 1.2 cherry {
157 1.18 rmind uint32_t masks = XEN_IPI_GENERIC | XEN_IPI_HVCB | XEN_IPI_XCALL |
158 1.6 cherry XEN_IPI_DDB | XEN_IPI_SYNCH_FPU |
159 1.38 bouyer XEN_IPI_HALT | XEN_IPI_AST | XEN_IPI_KPREEMPT;
160 1.2 cherry
161 1.2 cherry if (ipimask & ~masks) {
162 1.2 cherry return false;
163 1.3 cherry } else {
164 1.2 cherry return true;
165 1.2 cherry }
166 1.2 cherry
167 1.2 cherry }
168 1.2 cherry
169 1.2 cherry int
170 1.2 cherry xen_send_ipi(struct cpu_info *ci, uint32_t ipimask)
171 1.2 cherry {
172 1.2 cherry evtchn_port_t evtchn;
173 1.2 cherry
174 1.26 bouyer KASSERT(ci != NULL && ci != curcpu());
175 1.2 cherry
176 1.4 cherry if ((ci->ci_flags & CPUF_RUNNING) == 0) {
177 1.2 cherry return ENOENT;
178 1.2 cherry }
179 1.2 cherry
180 1.2 cherry evtchn = ci->ci_ipi_evtchn;
181 1.3 cherry
182 1.3 cherry KASSERTMSG(valid_ipimask(ipimask) == true,
183 1.5 jym "xen_send_ipi() called with invalid ipimask\n");
184 1.2 cherry
185 1.2 cherry atomic_or_32(&ci->ci_ipis, ipimask);
186 1.2 cherry hypervisor_notify_via_evtchn(evtchn);
187 1.2 cherry
188 1.2 cherry return 0;
189 1.2 cherry }
190 1.2 cherry
191 1.2 cherry void
192 1.2 cherry xen_broadcast_ipi(uint32_t ipimask)
193 1.2 cherry {
194 1.2 cherry struct cpu_info *ci, *self = curcpu();
195 1.2 cherry CPU_INFO_ITERATOR cii;
196 1.2 cherry
197 1.3 cherry KASSERTMSG(valid_ipimask(ipimask) == true,
198 1.5 jym "xen_broadcast_ipi() called with invalid ipimask\n");
199 1.2 cherry
200 1.2 cherry /*
201 1.2 cherry * XXX-cherry: there's an implicit broadcast sending order
202 1.2 cherry * which I dislike. Randomise this ? :-)
203 1.2 cherry */
204 1.2 cherry
205 1.2 cherry for (CPU_INFO_FOREACH(cii, ci)) {
206 1.2 cherry if (ci == NULL)
207 1.2 cherry continue;
208 1.2 cherry if (ci == self)
209 1.2 cherry continue;
210 1.2 cherry if (ci->ci_data.cpu_idlelwp == NULL)
211 1.2 cherry continue;
212 1.2 cherry if ((ci->ci_flags & CPUF_PRESENT) == 0)
213 1.2 cherry continue;
214 1.2 cherry if (ci->ci_flags & (CPUF_RUNNING)) {
215 1.2 cherry if (0 != xen_send_ipi(ci, ipimask)) {
216 1.2 cherry panic("xen_ipi of %x from %s to %s failed\n",
217 1.2 cherry ipimask, cpu_name(curcpu()),
218 1.2 cherry cpu_name(ci));
219 1.2 cherry }
220 1.2 cherry }
221 1.2 cherry }
222 1.2 cherry }
223 1.2 cherry
224 1.2 cherry /* MD wrapper for the xcall(9) callback. */
225 1.2 cherry
226 1.2 cherry static void
227 1.2 cherry xen_ipi_halt(struct cpu_info *ci, struct intrframe *intrf)
228 1.2 cherry {
229 1.2 cherry KASSERT(ci == curcpu());
230 1.2 cherry KASSERT(ci != NULL);
231 1.38 bouyer if (HYPERVISOR_vcpu_op(VCPUOP_down, ci->ci_vcpuid, NULL)) {
232 1.25 jdolecek panic("%s shutdown failed.\n", device_xname(ci->ci_dev));
233 1.2 cherry }
234 1.2 cherry
235 1.2 cherry }
236 1.2 cherry
237 1.2 cherry static void
238 1.14 christos xen_ipi_synch_fpu(struct cpu_info *ci, struct intrframe *intrf)
239 1.14 christos {
240 1.14 christos KASSERT(ci != NULL);
241 1.14 christos KASSERT(intrf != NULL);
242 1.14 christos
243 1.33 maxv panic("%s: impossible", __func__);
244 1.14 christos }
245 1.14 christos
246 1.24 jdolecek #ifdef DDB
247 1.14 christos static void
248 1.2 cherry xen_ipi_ddb(struct cpu_info *ci, struct intrframe *intrf)
249 1.2 cherry {
250 1.2 cherry KASSERT(ci != NULL);
251 1.2 cherry KASSERT(intrf != NULL);
252 1.2 cherry
253 1.2 cherry #ifdef __x86_64__
254 1.2 cherry ddb_ipi(intrf->if_tf);
255 1.2 cherry #else
256 1.2 cherry struct trapframe tf;
257 1.2 cherry tf.tf_gs = intrf->if_gs;
258 1.2 cherry tf.tf_fs = intrf->if_fs;
259 1.2 cherry tf.tf_es = intrf->if_es;
260 1.2 cherry tf.tf_ds = intrf->if_ds;
261 1.2 cherry tf.tf_edi = intrf->if_edi;
262 1.2 cherry tf.tf_esi = intrf->if_esi;
263 1.2 cherry tf.tf_ebp = intrf->if_ebp;
264 1.2 cherry tf.tf_ebx = intrf->if_ebx;
265 1.2 cherry tf.tf_ecx = intrf->if_ecx;
266 1.2 cherry tf.tf_eax = intrf->if_eax;
267 1.2 cherry tf.tf_trapno = intrf->__if_trapno;
268 1.2 cherry tf.tf_err = intrf->__if_err;
269 1.2 cherry tf.tf_eip = intrf->if_eip;
270 1.2 cherry tf.tf_cs = intrf->if_cs;
271 1.2 cherry tf.tf_eflags = intrf->if_eflags;
272 1.2 cherry tf.tf_esp = intrf->if_esp;
273 1.2 cherry tf.tf_ss = intrf->if_ss;
274 1.2 cherry
275 1.22 maxv ddb_ipi(tf);
276 1.2 cherry #endif
277 1.2 cherry }
278 1.24 jdolecek #endif /* DDB */
279 1.2 cherry
280 1.2 cherry static void
281 1.2 cherry xen_ipi_xcall(struct cpu_info *ci, struct intrframe *intrf)
282 1.2 cherry {
283 1.2 cherry KASSERT(ci != NULL);
284 1.2 cherry KASSERT(intrf != NULL);
285 1.2 cherry
286 1.2 cherry xc_ipi_handler();
287 1.2 cherry }
288 1.2 cherry
289 1.34 ad static void
290 1.34 ad xen_ipi_ast(struct cpu_info *ci, struct intrframe *intrf)
291 1.34 ad {
292 1.34 ad KASSERT(ci != NULL);
293 1.34 ad KASSERT(intrf != NULL);
294 1.34 ad
295 1.35 ad aston(ci->ci_onproc);
296 1.34 ad }
297 1.34 ad
298 1.38 bouyer static void
299 1.38 bouyer xen_ipi_generic(struct cpu_info *ci, struct intrframe *intrf)
300 1.38 bouyer {
301 1.38 bouyer KASSERT(ci != NULL);
302 1.38 bouyer KASSERT(intrf != NULL);
303 1.38 bouyer ipi_cpu_handler();
304 1.38 bouyer }
305 1.38 bouyer
306 1.38 bouyer static void
307 1.38 bouyer xen_ipi_hvcb(struct cpu_info *ci, struct intrframe *intrf)
308 1.38 bouyer {
309 1.38 bouyer KASSERT(ci != NULL);
310 1.38 bouyer KASSERT(intrf != NULL);
311 1.38 bouyer KASSERT(ci == curcpu());
312 1.38 bouyer KASSERT(!ci->ci_vcpu->evtchn_upcall_mask);
313 1.38 bouyer
314 1.38 bouyer hypervisor_force_callback();
315 1.38 bouyer }
316 1.38 bouyer
317 1.38 bouyer static void
318 1.38 bouyer xen_ipi_kpreempt(struct cpu_info *ci, struct intrframe * intrf)
319 1.38 bouyer {
320 1.38 bouyer softint_trigger(1 << SIR_PREEMPT);
321 1.38 bouyer }
322 1.38 bouyer
323 1.38 bouyer #ifdef XENPV
324 1.2 cherry void
325 1.2 cherry xc_send_ipi(struct cpu_info *ci)
326 1.2 cherry {
327 1.2 cherry
328 1.2 cherry KASSERT(kpreempt_disabled());
329 1.2 cherry KASSERT(curcpu() != ci);
330 1.2 cherry if (ci) {
331 1.2 cherry if (0 != xen_send_ipi(ci, XEN_IPI_XCALL)) {
332 1.2 cherry panic("xen_send_ipi(XEN_IPI_XCALL) failed\n");
333 1.3 cherry }
334 1.2 cherry } else {
335 1.2 cherry xen_broadcast_ipi(XEN_IPI_XCALL);
336 1.2 cherry }
337 1.2 cherry }
338 1.6 cherry
339 1.18 rmind void
340 1.18 rmind cpu_ipi(struct cpu_info *ci)
341 1.18 rmind {
342 1.18 rmind KASSERT(kpreempt_disabled());
343 1.18 rmind KASSERT(curcpu() != ci);
344 1.18 rmind if (ci) {
345 1.18 rmind if (0 != xen_send_ipi(ci, XEN_IPI_GENERIC)) {
346 1.18 rmind panic("xen_send_ipi(XEN_IPI_GENERIC) failed\n");
347 1.18 rmind }
348 1.18 rmind } else {
349 1.18 rmind xen_broadcast_ipi(XEN_IPI_GENERIC);
350 1.18 rmind }
351 1.18 rmind }
352 1.38 bouyer #endif /* XENPV */
353