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