xen_ipi.c revision 1.25.2.4 1 /* $NetBSD: xen_ipi.c,v 1.25.2.4 2020/04/21 18:42:13 martin 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 * __KERNEL_RCSID(0, "$NetBSD: xen_ipi.c,v 1.25.2.4 2020/04/21 18:42:13 martin Exp $");
37 */
38
39 __KERNEL_RCSID(0, "$NetBSD: xen_ipi.c,v 1.25.2.4 2020/04/21 18:42:13 martin 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 static void xen_ipi_ast(struct cpu_info *, struct intrframe *);
75
76 static void (*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 };
90
91 static int
92 xen_ipi_handler(void *arg)
93 {
94 uint32_t pending;
95 int bit;
96 struct cpu_info *ci;
97 struct intrframe *regs;
98
99 ci = curcpu();
100 regs = arg;
101
102 KASSERT(ci == arg);
103
104 pending = atomic_swap_32(&ci->ci_ipis, 0);
105
106 KDASSERT((pending >> XEN_NIPIS) == 0);
107 while ((bit = ffs(pending)) != 0) {
108 bit--;
109 pending &= ~(1 << bit);
110 ci->ci_ipi_events[bit].ev_count++;
111 if (ipifunc[bit] != NULL) {
112 (*ipifunc[bit])(ci, regs);
113 } else {
114 panic("ipifunc[%d] unsupported!\n", bit);
115 /* NOTREACHED */
116 }
117 }
118
119 return 0;
120 }
121
122 /* Must be called once for every cpu that expects to send/recv ipis */
123 void
124 xen_ipi_init(void)
125 {
126 cpuid_t vcpu;
127 evtchn_port_t evtchn;
128 struct cpu_info *ci;
129 char intr_xname[INTRDEVNAMEBUF];
130
131 ci = curcpu();
132
133 vcpu = ci->ci_cpuid;
134 KASSERT(vcpu < XEN_LEGACY_MAX_VCPUS);
135
136 evtchn = bind_vcpu_to_evtch(vcpu);
137 ci->ci_ipi_evtchn = evtchn;
138
139 KASSERT(evtchn != -1 && evtchn < NR_EVENT_CHANNELS);
140
141 snprintf(intr_xname, sizeof(intr_xname), "%s ipi",
142 device_xname(ci->ci_dev));
143
144 if (event_set_handler(evtchn, xen_ipi_handler, ci, IPL_HIGH, NULL,
145 intr_xname, true, false) != 0) {
146 panic("%s: unable to register ipi handler\n", __func__);
147 /* NOTREACHED */
148 }
149
150 hypervisor_unmask_event(evtchn);
151 }
152
153 #ifdef DIAGNOSTIC
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_KICK | XEN_IPI_AST;
160
161 if (ipimask & ~masks) {
162 return false;
163 } else {
164 return true;
165 }
166
167 }
168 #endif
169
170 int
171 xen_send_ipi(struct cpu_info *ci, uint32_t ipimask)
172 {
173 evtchn_port_t evtchn;
174
175 KASSERT(ci != NULL && ci != curcpu());
176
177 if ((ci->ci_flags & CPUF_RUNNING) == 0) {
178 return ENOENT;
179 }
180
181 evtchn = ci->ci_ipi_evtchn;
182
183 KASSERTMSG(valid_ipimask(ipimask) == true,
184 "xen_send_ipi() called with invalid ipimask\n");
185
186 atomic_or_32(&ci->ci_ipis, ipimask);
187 hypervisor_notify_via_evtchn(evtchn);
188
189 return 0;
190 }
191
192 void
193 xen_broadcast_ipi(uint32_t ipimask)
194 {
195 struct cpu_info *ci, *self = curcpu();
196 CPU_INFO_ITERATOR cii;
197
198 KASSERTMSG(valid_ipimask(ipimask) == true,
199 "xen_broadcast_ipi() called with invalid ipimask\n");
200
201 /*
202 * XXX-cherry: there's an implicit broadcast sending order
203 * which I dislike. Randomise this ? :-)
204 */
205
206 for (CPU_INFO_FOREACH(cii, ci)) {
207 if (ci == NULL)
208 continue;
209 if (ci == self)
210 continue;
211 if (ci->ci_data.cpu_idlelwp == NULL)
212 continue;
213 if ((ci->ci_flags & CPUF_PRESENT) == 0)
214 continue;
215 if (ci->ci_flags & (CPUF_RUNNING)) {
216 if (0 != xen_send_ipi(ci, ipimask)) {
217 panic("xen_ipi of %x from %s to %s failed\n",
218 ipimask, cpu_name(curcpu()),
219 cpu_name(ci));
220 }
221 }
222 }
223 }
224
225 /* MD wrapper for the xcall(9) callback. */
226
227 static void
228 xen_ipi_halt(struct cpu_info *ci, struct intrframe *intrf)
229 {
230 KASSERT(ci == curcpu());
231 KASSERT(ci != NULL);
232 if (HYPERVISOR_vcpu_op(VCPUOP_down, ci->ci_cpuid, NULL)) {
233 panic("%s shutdown failed.\n", device_xname(ci->ci_dev));
234 }
235
236 }
237
238 static void
239 xen_ipi_synch_fpu(struct cpu_info *ci, struct intrframe *intrf)
240 {
241 KASSERT(ci != NULL);
242 KASSERT(intrf != NULL);
243
244 panic("%s: impossible", __func__);
245 }
246
247 #ifdef DDB
248 static void
249 xen_ipi_ddb(struct cpu_info *ci, struct intrframe *intrf)
250 {
251 KASSERT(ci != NULL);
252 KASSERT(intrf != NULL);
253
254 #ifdef __x86_64__
255 ddb_ipi(intrf->if_tf);
256 #else
257 struct trapframe tf;
258 tf.tf_gs = intrf->if_gs;
259 tf.tf_fs = intrf->if_fs;
260 tf.tf_es = intrf->if_es;
261 tf.tf_ds = intrf->if_ds;
262 tf.tf_edi = intrf->if_edi;
263 tf.tf_esi = intrf->if_esi;
264 tf.tf_ebp = intrf->if_ebp;
265 tf.tf_ebx = intrf->if_ebx;
266 tf.tf_ecx = intrf->if_ecx;
267 tf.tf_eax = intrf->if_eax;
268 tf.tf_trapno = intrf->__if_trapno;
269 tf.tf_err = intrf->__if_err;
270 tf.tf_eip = intrf->if_eip;
271 tf.tf_cs = intrf->if_cs;
272 tf.tf_eflags = intrf->if_eflags;
273 tf.tf_esp = intrf->if_esp;
274 tf.tf_ss = intrf->if_ss;
275
276 ddb_ipi(tf);
277 #endif
278 }
279 #endif /* DDB */
280
281 static void
282 xen_ipi_xcall(struct cpu_info *ci, struct intrframe *intrf)
283 {
284 KASSERT(ci != NULL);
285 KASSERT(intrf != NULL);
286
287 xc_ipi_handler();
288 }
289
290 static void
291 xen_ipi_ast(struct cpu_info *ci, struct intrframe *intrf)
292 {
293 KASSERT(ci != NULL);
294 KASSERT(intrf != NULL);
295
296 aston(ci->ci_onproc);
297 }
298
299 void
300 xc_send_ipi(struct cpu_info *ci)
301 {
302
303 KASSERT(kpreempt_disabled());
304 KASSERT(curcpu() != ci);
305 if (ci) {
306 if (0 != xen_send_ipi(ci, XEN_IPI_XCALL)) {
307 panic("xen_send_ipi(XEN_IPI_XCALL) failed\n");
308 }
309 } else {
310 xen_broadcast_ipi(XEN_IPI_XCALL);
311 }
312 }
313
314 static void
315 xen_ipi_generic(struct cpu_info *ci, struct intrframe *intrf)
316 {
317 KASSERT(ci != NULL);
318 KASSERT(intrf != NULL);
319 ipi_cpu_handler();
320 }
321
322 void
323 cpu_ipi(struct cpu_info *ci)
324 {
325 KASSERT(kpreempt_disabled());
326 KASSERT(curcpu() != ci);
327 if (ci) {
328 if (0 != xen_send_ipi(ci, XEN_IPI_GENERIC)) {
329 panic("xen_send_ipi(XEN_IPI_GENERIC) failed\n");
330 }
331 } else {
332 xen_broadcast_ipi(XEN_IPI_GENERIC);
333 }
334 }
335
336 static void
337 xen_ipi_hvcb(struct cpu_info *ci, struct intrframe *intrf)
338 {
339 KASSERT(ci != NULL);
340 KASSERT(intrf != NULL);
341 KASSERT(ci == curcpu());
342 KASSERT(!ci->ci_vcpu->evtchn_upcall_mask);
343
344 hypervisor_force_callback();
345 }
346