xen_ipi.c revision 1.35.6.5 1 /* $NetBSD: xen_ipi.c,v 1.35.6.5 2020/04/20 11:29:01 bouyer 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.35.6.5 2020/04/20 11:29:01 bouyer 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, false) != 0) {
147 panic("%s: unable to register ipi handler\n", __func__);
148 /* NOTREACHED */
149 }
150
151 hypervisor_unmask_event(evtchn);
152 }
153
154 #ifdef DIAGNOSTIC
155 static inline bool /* helper */
156 valid_ipimask(uint32_t ipimask)
157 {
158 uint32_t masks = XEN_IPI_GENERIC | XEN_IPI_HVCB | XEN_IPI_XCALL |
159 XEN_IPI_DDB | XEN_IPI_SYNCH_FPU |
160 XEN_IPI_HALT | XEN_IPI_KICK | XEN_IPI_AST | XEN_IPI_KPREEMPT;
161
162 if (ipimask & ~masks) {
163 return false;
164 } else {
165 return true;
166 }
167
168 }
169 #endif
170
171 int
172 xen_send_ipi(struct cpu_info *ci, uint32_t ipimask)
173 {
174 evtchn_port_t evtchn;
175
176 KASSERT(ci != NULL && ci != curcpu());
177
178 if ((ci->ci_flags & CPUF_RUNNING) == 0) {
179 return ENOENT;
180 }
181
182 evtchn = ci->ci_ipi_evtchn;
183
184 KASSERTMSG(valid_ipimask(ipimask) == true,
185 "xen_send_ipi() called with invalid ipimask\n");
186
187 atomic_or_32(&ci->ci_ipis, ipimask);
188 hypervisor_notify_via_evtchn(evtchn);
189
190 return 0;
191 }
192
193 void
194 xen_broadcast_ipi(uint32_t ipimask)
195 {
196 struct cpu_info *ci, *self = curcpu();
197 CPU_INFO_ITERATOR cii;
198
199 KASSERTMSG(valid_ipimask(ipimask) == true,
200 "xen_broadcast_ipi() called with invalid ipimask\n");
201
202 /*
203 * XXX-cherry: there's an implicit broadcast sending order
204 * which I dislike. Randomise this ? :-)
205 */
206
207 for (CPU_INFO_FOREACH(cii, ci)) {
208 if (ci == NULL)
209 continue;
210 if (ci == self)
211 continue;
212 if (ci->ci_data.cpu_idlelwp == NULL)
213 continue;
214 if ((ci->ci_flags & CPUF_PRESENT) == 0)
215 continue;
216 if (ci->ci_flags & (CPUF_RUNNING)) {
217 if (0 != xen_send_ipi(ci, ipimask)) {
218 panic("xen_ipi of %x from %s to %s failed\n",
219 ipimask, cpu_name(curcpu()),
220 cpu_name(ci));
221 }
222 }
223 }
224 }
225
226 /* MD wrapper for the xcall(9) callback. */
227
228 static void
229 xen_ipi_halt(struct cpu_info *ci, struct intrframe *intrf)
230 {
231 KASSERT(ci == curcpu());
232 KASSERT(ci != NULL);
233 if (HYPERVISOR_vcpu_op(VCPUOP_down, ci->ci_vcpuid, NULL)) {
234 panic("%s shutdown failed.\n", device_xname(ci->ci_dev));
235 }
236
237 }
238
239 static void
240 xen_ipi_synch_fpu(struct cpu_info *ci, struct intrframe *intrf)
241 {
242 KASSERT(ci != NULL);
243 KASSERT(intrf != NULL);
244
245 panic("%s: impossible", __func__);
246 }
247
248 #ifdef DDB
249 static void
250 xen_ipi_ddb(struct cpu_info *ci, struct intrframe *intrf)
251 {
252 KASSERT(ci != NULL);
253 KASSERT(intrf != NULL);
254
255 #ifdef __x86_64__
256 ddb_ipi(intrf->if_tf);
257 #else
258 struct trapframe tf;
259 tf.tf_gs = intrf->if_gs;
260 tf.tf_fs = intrf->if_fs;
261 tf.tf_es = intrf->if_es;
262 tf.tf_ds = intrf->if_ds;
263 tf.tf_edi = intrf->if_edi;
264 tf.tf_esi = intrf->if_esi;
265 tf.tf_ebp = intrf->if_ebp;
266 tf.tf_ebx = intrf->if_ebx;
267 tf.tf_ecx = intrf->if_ecx;
268 tf.tf_eax = intrf->if_eax;
269 tf.tf_trapno = intrf->__if_trapno;
270 tf.tf_err = intrf->__if_err;
271 tf.tf_eip = intrf->if_eip;
272 tf.tf_cs = intrf->if_cs;
273 tf.tf_eflags = intrf->if_eflags;
274 tf.tf_esp = intrf->if_esp;
275 tf.tf_ss = intrf->if_ss;
276
277 ddb_ipi(tf);
278 #endif
279 }
280 #endif /* DDB */
281
282 static void
283 xen_ipi_xcall(struct cpu_info *ci, struct intrframe *intrf)
284 {
285 KASSERT(ci != NULL);
286 KASSERT(intrf != NULL);
287
288 xc_ipi_handler();
289 }
290
291 static void
292 xen_ipi_ast(struct cpu_info *ci, struct intrframe *intrf)
293 {
294 KASSERT(ci != NULL);
295 KASSERT(intrf != NULL);
296
297 aston(ci->ci_onproc);
298 }
299
300 static void
301 xen_ipi_generic(struct cpu_info *ci, struct intrframe *intrf)
302 {
303 KASSERT(ci != NULL);
304 KASSERT(intrf != NULL);
305 ipi_cpu_handler();
306 }
307
308 static void
309 xen_ipi_hvcb(struct cpu_info *ci, struct intrframe *intrf)
310 {
311 KASSERT(ci != NULL);
312 KASSERT(intrf != NULL);
313 KASSERT(ci == curcpu());
314 KASSERT(!ci->ci_vcpu->evtchn_upcall_mask);
315
316 hypervisor_force_callback();
317 }
318
319 static void
320 xen_ipi_kpreempt(struct cpu_info *ci, struct intrframe * intrf)
321 {
322 softint_trigger(1 << SIR_PREEMPT);
323 }
324
325 #ifdef XENPV
326 void
327 xc_send_ipi(struct cpu_info *ci)
328 {
329
330 KASSERT(kpreempt_disabled());
331 KASSERT(curcpu() != ci);
332 if (ci) {
333 if (0 != xen_send_ipi(ci, XEN_IPI_XCALL)) {
334 panic("xen_send_ipi(XEN_IPI_XCALL) failed\n");
335 }
336 } else {
337 xen_broadcast_ipi(XEN_IPI_XCALL);
338 }
339 }
340
341 void
342 cpu_ipi(struct cpu_info *ci)
343 {
344 KASSERT(kpreempt_disabled());
345 KASSERT(curcpu() != ci);
346 if (ci) {
347 if (0 != xen_send_ipi(ci, XEN_IPI_GENERIC)) {
348 panic("xen_send_ipi(XEN_IPI_GENERIC) failed\n");
349 }
350 } else {
351 xen_broadcast_ipi(XEN_IPI_GENERIC);
352 }
353 }
354 #endif /* XENPV */
355