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