xen_ipi.c revision 1.40 1 /* $NetBSD: xen_ipi.c,v 1.40 2022/01/05 20:21:29 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2011, 2019 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 */
37
38 __KERNEL_RCSID(0, "$NetBSD: xen_ipi.c,v 1.40 2022/01/05 20:21:29 christos Exp $");
39
40 #include "opt_ddb.h"
41
42 #include <sys/types.h>
43
44 #include <sys/atomic.h>
45 #include <sys/cpu.h>
46 #include <sys/mutex.h>
47 #include <sys/device.h>
48 #include <sys/xcall.h>
49 #include <sys/ipi.h>
50 #include <sys/errno.h>
51 #include <sys/systm.h>
52
53 #include <x86/fpu.h>
54 #include <machine/frame.h>
55 #include <machine/segments.h>
56
57 #include <xen/evtchn.h>
58 #include <xen/intr.h>
59 #include <xen/intrdefs.h>
60 #include <xen/hypervisor.h>
61 #include <xen/include/public/vcpu.h>
62
63 #ifdef DDB
64 extern void ddb_ipi(struct trapframe);
65 static void xen_ipi_ddb(struct cpu_info *, struct intrframe *);
66 #endif
67
68 static void xen_ipi_halt(struct cpu_info *, struct intrframe *);
69 static void xen_ipi_synch_fpu(struct cpu_info *, struct intrframe *);
70 static void xen_ipi_xcall(struct cpu_info *, struct intrframe *);
71 static void xen_ipi_hvcb(struct cpu_info *, struct intrframe *);
72 static void xen_ipi_generic(struct cpu_info *, struct intrframe *);
73 static void xen_ipi_ast(struct cpu_info *, struct intrframe *);
74 static void xen_ipi_kpreempt(struct cpu_info *ci, struct intrframe *);
75
76 static void (*xen_ipifunc[XEN_NIPIS])(struct cpu_info *, struct intrframe *) =
77 { /* In order of priority (see: xen/include/intrdefs.h */
78 xen_ipi_halt,
79 xen_ipi_synch_fpu,
80 #ifdef DDB
81 xen_ipi_ddb,
82 #else
83 NULL,
84 #endif
85 xen_ipi_xcall,
86 xen_ipi_hvcb,
87 xen_ipi_generic,
88 xen_ipi_ast,
89 xen_ipi_kpreempt
90 };
91
92 static int
93 xen_ipi_handler(void *arg)
94 {
95 uint32_t pending;
96 int bit;
97 struct cpu_info *ci;
98 struct intrframe *regs;
99
100 ci = curcpu();
101 regs = arg;
102
103 KASSERT(ci == arg);
104
105 pending = atomic_swap_32(&ci->ci_ipis, 0);
106
107 KDASSERT((pending >> XEN_NIPIS) == 0);
108 while ((bit = ffs(pending)) != 0) {
109 bit--;
110 pending &= ~(1 << bit);
111 ci->ci_ipi_events[bit].ev_count++;
112 if (xen_ipifunc[bit] != NULL) {
113 (*xen_ipifunc[bit])(ci, regs);
114 } else {
115 panic("xen_ipifunc[%d] unsupported!\n", bit);
116 /* NOTREACHED */
117 }
118 }
119
120 return 0;
121 }
122
123 /* Must be called once for every cpu that expects to send/recv ipis */
124 void
125 xen_ipi_init(void)
126 {
127 cpuid_t vcpu;
128 evtchn_port_t evtchn;
129 struct cpu_info *ci;
130 char intr_xname[INTRDEVNAMEBUF];
131
132 ci = curcpu();
133
134 vcpu = ci->ci_vcpuid;
135 KASSERT(vcpu < XEN_LEGACY_MAX_VCPUS);
136
137 evtchn = bind_vcpu_to_evtch(vcpu);
138 ci->ci_ipi_evtchn = evtchn;
139
140 KASSERT(evtchn != -1 && evtchn < NR_EVENT_CHANNELS);
141
142 snprintf(intr_xname, sizeof(intr_xname), "%s ipi",
143 device_xname(ci->ci_dev));
144
145 if (event_set_handler(evtchn, xen_ipi_handler, ci, IPL_HIGH, NULL,
146 intr_xname, true, ci) == NULL) {
147 panic("%s: unable to register ipi handler\n", __func__);
148 /* NOTREACHED */
149 }
150
151 hypervisor_unmask_event(evtchn);
152 }
153
154 static inline bool /* helper */
155 valid_ipimask(uint32_t ipimask)
156 {
157 uint32_t masks = XEN_IPI_GENERIC | XEN_IPI_HVCB | XEN_IPI_XCALL |
158 XEN_IPI_DDB | XEN_IPI_SYNCH_FPU |
159 XEN_IPI_HALT | XEN_IPI_AST | XEN_IPI_KPREEMPT;
160
161 if (ipimask & ~masks) {
162 return false;
163 } else {
164 return true;
165 }
166
167 }
168
169 int
170 xen_send_ipi(struct cpu_info *ci, uint32_t ipimask)
171 {
172 evtchn_port_t evtchn;
173
174 KASSERT(ci != NULL && ci != curcpu());
175
176 if ((ci->ci_flags & CPUF_RUNNING) == 0) {
177 return ENOENT;
178 }
179
180 evtchn = ci->ci_ipi_evtchn;
181
182 KASSERTMSG(valid_ipimask(ipimask) == true,
183 "xen_send_ipi() called with invalid ipimask\n");
184
185 atomic_or_32(&ci->ci_ipis, ipimask);
186 hypervisor_notify_via_evtchn(evtchn);
187
188 return 0;
189 }
190
191 void
192 xen_broadcast_ipi(uint32_t ipimask)
193 {
194 struct cpu_info *ci, *self = curcpu();
195 CPU_INFO_ITERATOR cii;
196
197 KASSERTMSG(valid_ipimask(ipimask) == true,
198 "xen_broadcast_ipi() called with invalid ipimask\n");
199
200 /*
201 * XXX-cherry: there's an implicit broadcast sending order
202 * which I dislike. Randomise this ? :-)
203 */
204
205 for (CPU_INFO_FOREACH(cii, ci)) {
206 if (ci == NULL)
207 continue;
208 if (ci == self)
209 continue;
210 if (ci->ci_data.cpu_idlelwp == NULL)
211 continue;
212 if ((ci->ci_flags & CPUF_PRESENT) == 0)
213 continue;
214 if (ci->ci_flags & (CPUF_RUNNING)) {
215 if (0 != xen_send_ipi(ci, ipimask)) {
216 panic("xen_ipi of %x from %s to %s failed\n",
217 ipimask, cpu_name(curcpu()),
218 cpu_name(ci));
219 }
220 }
221 }
222 }
223
224 /* MD wrapper for the xcall(9) callback. */
225
226 static void
227 xen_ipi_halt(struct cpu_info *ci, struct intrframe *intrf)
228 {
229 KASSERT(ci == curcpu());
230 KASSERT(ci != NULL);
231 if (HYPERVISOR_vcpu_op(VCPUOP_down, ci->ci_vcpuid, NULL)) {
232 panic("%s shutdown failed.\n", device_xname(ci->ci_dev));
233 }
234
235 }
236
237 static void
238 xen_ipi_synch_fpu(struct cpu_info *ci, struct intrframe *intrf)
239 {
240 KASSERT(ci != NULL);
241 KASSERT(intrf != NULL);
242
243 panic("%s: impossible", __func__);
244 }
245
246 #ifdef DDB
247 static void
248 xen_ipi_ddb(struct cpu_info *ci, struct intrframe *intrf)
249 {
250 KASSERT(ci != NULL);
251 KASSERT(intrf != NULL);
252
253 #ifdef __x86_64__
254 ddb_ipi(intrf->if_tf);
255 #else
256 struct trapframe tf;
257 tf.tf_gs = intrf->if_gs;
258 tf.tf_fs = intrf->if_fs;
259 tf.tf_es = intrf->if_es;
260 tf.tf_ds = intrf->if_ds;
261 tf.tf_edi = intrf->if_edi;
262 tf.tf_esi = intrf->if_esi;
263 tf.tf_ebp = intrf->if_ebp;
264 tf.tf_ebx = intrf->if_ebx;
265 tf.tf_ecx = intrf->if_ecx;
266 tf.tf_eax = intrf->if_eax;
267 tf.tf_trapno = intrf->__if_trapno;
268 tf.tf_err = intrf->__if_err;
269 tf.tf_eip = intrf->if_eip;
270 tf.tf_cs = intrf->if_cs;
271 tf.tf_eflags = intrf->if_eflags;
272 tf.tf_esp = intrf->if_esp;
273 tf.tf_ss = intrf->if_ss;
274
275 ddb_ipi(tf);
276 #endif
277 }
278 #endif /* DDB */
279
280 static void
281 xen_ipi_xcall(struct cpu_info *ci, struct intrframe *intrf)
282 {
283 KASSERT(ci != NULL);
284 KASSERT(intrf != NULL);
285
286 xc_ipi_handler();
287 }
288
289 static void
290 xen_ipi_ast(struct cpu_info *ci, struct intrframe *intrf)
291 {
292 KASSERT(ci != NULL);
293 KASSERT(intrf != NULL);
294
295 aston(ci->ci_onproc);
296 }
297
298 static void
299 xen_ipi_generic(struct cpu_info *ci, struct intrframe *intrf)
300 {
301 KASSERT(ci != NULL);
302 KASSERT(intrf != NULL);
303 ipi_cpu_handler();
304 }
305
306 static void
307 xen_ipi_hvcb(struct cpu_info *ci, struct intrframe *intrf)
308 {
309 KASSERT(ci != NULL);
310 KASSERT(intrf != NULL);
311 KASSERT(ci == curcpu());
312 KASSERT(!ci->ci_vcpu->evtchn_upcall_mask);
313
314 hypervisor_force_callback();
315 }
316
317 static void
318 xen_ipi_kpreempt(struct cpu_info *ci, struct intrframe * intrf)
319 {
320 softint_trigger(1 << SIR_PREEMPT);
321 }
322
323 #ifdef XENPV
324 void
325 xc_send_ipi(struct cpu_info *ci)
326 {
327
328 KASSERT(kpreempt_disabled());
329 KASSERT(curcpu() != ci);
330 if (ci) {
331 if (0 != xen_send_ipi(ci, XEN_IPI_XCALL)) {
332 panic("xen_send_ipi(XEN_IPI_XCALL) failed\n");
333 }
334 } else {
335 xen_broadcast_ipi(XEN_IPI_XCALL);
336 }
337 }
338
339 void
340 cpu_ipi(struct cpu_info *ci)
341 {
342 KASSERT(kpreempt_disabled());
343 KASSERT(curcpu() != ci);
344 if (ci) {
345 if (0 != xen_send_ipi(ci, XEN_IPI_GENERIC)) {
346 panic("xen_send_ipi(XEN_IPI_GENERIC) failed\n");
347 }
348 } else {
349 xen_broadcast_ipi(XEN_IPI_GENERIC);
350 }
351 }
352 #endif /* XENPV */
353