xen_ipi.c revision 1.20.10.1 1 /* $NetBSD: xen_ipi.c,v 1.20.10.1 2021/04/30 14:22:24 martin 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.20.10.1 2021/04/30 14:22:24 martin Exp $");
37 */
38
39 __KERNEL_RCSID(0, "$NetBSD: xen_ipi.c,v 1.20.10.1 2021/04/30 14:22:24 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/xen-public/vcpu.h>
63
64 #ifdef DDB
65 #ifdef __x86_64__
66 extern void ddb_ipi(struct trapframe);
67 #else
68 extern void ddb_ipi(int, struct trapframe);
69 #endif /* __x86_64__ */
70 static void xen_ipi_ddb(struct cpu_info *, struct intrframe *);
71 #endif
72
73 static void xen_ipi_halt(struct cpu_info *, struct intrframe *);
74 static void xen_ipi_synch_fpu(struct cpu_info *, struct intrframe *);
75 static void xen_ipi_xcall(struct cpu_info *, struct intrframe *);
76 static void xen_ipi_hvcb(struct cpu_info *, struct intrframe *);
77 static void xen_ipi_generic(struct cpu_info *, struct intrframe *);
78
79 static void (*ipifunc[XEN_NIPIS])(struct cpu_info *, struct intrframe *) =
80 { /* In order of priority (see: xen/include/intrdefs.h */
81 xen_ipi_halt,
82 xen_ipi_synch_fpu,
83 #ifdef DDB
84 xen_ipi_ddb,
85 #else
86 NULL,
87 #endif
88 xen_ipi_xcall,
89 xen_ipi_hvcb,
90 xen_ipi_generic,
91 };
92
93 static void
94 xen_ipi_handler(struct cpu_info *ci, struct intrframe *regs)
95 {
96 uint32_t pending;
97 int bit;
98
99 pending = atomic_swap_32(&ci->ci_ipis, 0);
100
101 KDASSERT((pending >> XEN_NIPIS) == 0);
102 while ((bit = ffs(pending)) != 0) {
103 bit--;
104 pending &= ~(1 << bit);
105 ci->ci_ipi_events[bit].ev_count++;
106 if (ipifunc[bit] != NULL) {
107 (*ipifunc[bit])(ci, regs);
108 } else {
109 panic("ipifunc[%d] unsupported!\n", bit);
110 /* NOTREACHED */
111 }
112 }
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
123 ci = curcpu();
124
125 vcpu = ci->ci_cpuid;
126 KASSERT(vcpu < XEN_LEGACY_MAX_VCPUS);
127
128 evtchn = bind_vcpu_to_evtch(vcpu);
129 ci->ci_ipi_evtchn = evtchn;
130
131 KASSERT(evtchn != -1 && evtchn < NR_EVENT_CHANNELS);
132
133 if (0 != event_set_handler(evtchn, (int (*)(void *))xen_ipi_handler,
134 ci, IPL_HIGH, "ipi")) {
135 panic("event_set_handler(...) KPI violation\n");
136 /* NOTREACHED */
137 }
138
139 hypervisor_enable_event(evtchn);
140 }
141
142 #ifdef DIAGNOSTIC
143 static inline bool /* helper */
144 valid_ipimask(uint32_t ipimask)
145 {
146 uint32_t masks = XEN_IPI_GENERIC | XEN_IPI_HVCB | XEN_IPI_XCALL |
147 XEN_IPI_DDB | XEN_IPI_SYNCH_FPU |
148 XEN_IPI_HALT | XEN_IPI_KICK;
149
150 if (ipimask & ~masks) {
151 return false;
152 } else {
153 return true;
154 }
155
156 }
157 #endif
158
159 int
160 xen_send_ipi(struct cpu_info *ci, uint32_t ipimask)
161 {
162 evtchn_port_t evtchn;
163
164 KASSERT(ci != NULL || ci != curcpu());
165
166 if ((ci->ci_flags & CPUF_RUNNING) == 0) {
167 return ENOENT;
168 }
169
170 evtchn = ci->ci_ipi_evtchn;
171
172 KASSERTMSG(valid_ipimask(ipimask) == true,
173 "xen_send_ipi() called with invalid ipimask\n");
174
175 atomic_or_32(&ci->ci_ipis, ipimask);
176 hypervisor_notify_via_evtchn(evtchn);
177
178 return 0;
179 }
180
181 void
182 xen_broadcast_ipi(uint32_t ipimask)
183 {
184 struct cpu_info *ci, *self = curcpu();
185 CPU_INFO_ITERATOR cii;
186
187 KASSERTMSG(valid_ipimask(ipimask) == true,
188 "xen_broadcast_ipi() called with invalid ipimask\n");
189
190 /*
191 * XXX-cherry: there's an implicit broadcast sending order
192 * which I dislike. Randomise this ? :-)
193 */
194
195 for (CPU_INFO_FOREACH(cii, ci)) {
196 if (ci == NULL)
197 continue;
198 if (ci == self)
199 continue;
200 if (ci->ci_data.cpu_idlelwp == NULL)
201 continue;
202 if ((ci->ci_flags & CPUF_PRESENT) == 0)
203 continue;
204 if (ci->ci_flags & (CPUF_RUNNING)) {
205 if (0 != xen_send_ipi(ci, ipimask)) {
206 panic("xen_ipi of %x from %s to %s failed\n",
207 ipimask, cpu_name(curcpu()),
208 cpu_name(ci));
209 }
210 }
211 }
212 }
213
214 /* MD wrapper for the xcall(9) callback. */
215 #define PRIuCPUID "lu" /* XXX: move this somewhere more appropriate */
216
217 static void
218 xen_ipi_halt(struct cpu_info *ci, struct intrframe *intrf)
219 {
220 KASSERT(ci == curcpu());
221 KASSERT(ci != NULL);
222 if (HYPERVISOR_vcpu_op(VCPUOP_down, ci->ci_cpuid, NULL)) {
223 panic("vcpu%" PRIuCPUID "shutdown failed.\n", ci->ci_cpuid);
224 }
225
226 }
227
228 static void
229 xen_ipi_synch_fpu(struct cpu_info *ci, struct intrframe *intrf)
230 {
231 KASSERT(ci != NULL);
232 KASSERT(intrf != NULL);
233
234 fpusave_cpu(true);
235 }
236
237 #ifdef DDB
238 static void
239 xen_ipi_ddb(struct cpu_info *ci, struct intrframe *intrf)
240 {
241 KASSERT(ci != NULL);
242 KASSERT(intrf != NULL);
243
244 #ifdef __x86_64__
245 ddb_ipi(intrf->if_tf);
246 #else
247 struct trapframe tf;
248 tf.tf_gs = intrf->if_gs;
249 tf.tf_fs = intrf->if_fs;
250 tf.tf_es = intrf->if_es;
251 tf.tf_ds = intrf->if_ds;
252 tf.tf_edi = intrf->if_edi;
253 tf.tf_esi = intrf->if_esi;
254 tf.tf_ebp = intrf->if_ebp;
255 tf.tf_ebx = intrf->if_ebx;
256 tf.tf_ecx = intrf->if_ecx;
257 tf.tf_eax = intrf->if_eax;
258 tf.tf_trapno = intrf->__if_trapno;
259 tf.tf_err = intrf->__if_err;
260 tf.tf_eip = intrf->if_eip;
261 tf.tf_cs = intrf->if_cs;
262 tf.tf_eflags = intrf->if_eflags;
263 tf.tf_esp = intrf->if_esp;
264 tf.tf_ss = intrf->if_ss;
265
266 /* XXX: does i386/Xen have vm86 support ?
267 tf.tf_vm86_es;
268 tf.tf_vm86_ds;
269 tf.tf_vm86_fs;
270 tf.tf_vm86_gs;
271 :XXX */
272
273 ddb_ipi(SEL_KPL, tf);
274 #endif
275 }
276 #endif /* DDB */
277
278 static void
279 xen_ipi_xcall(struct cpu_info *ci, struct intrframe *intrf)
280 {
281 KASSERT(ci != NULL);
282 KASSERT(intrf != NULL);
283
284 xc_ipi_handler();
285 }
286
287 void
288 xc_send_ipi(struct cpu_info *ci)
289 {
290
291 KASSERT(kpreempt_disabled());
292 KASSERT(curcpu() != ci);
293 if (ci) {
294 if (0 != xen_send_ipi(ci, XEN_IPI_XCALL)) {
295 panic("xen_send_ipi(XEN_IPI_XCALL) failed\n");
296 }
297 } else {
298 xen_broadcast_ipi(XEN_IPI_XCALL);
299 }
300 }
301
302 static void
303 xen_ipi_generic(struct cpu_info *ci, struct intrframe *intrf)
304 {
305 KASSERT(ci != NULL);
306 KASSERT(intrf != NULL);
307
308 ipi_cpu_handler();
309 }
310
311 void
312 cpu_ipi(struct cpu_info *ci)
313 {
314 KASSERT(kpreempt_disabled());
315 KASSERT(curcpu() != ci);
316 if (ci) {
317 if (0 != xen_send_ipi(ci, XEN_IPI_GENERIC)) {
318 panic("xen_send_ipi(XEN_IPI_GENERIC) failed\n");
319 }
320 } else {
321 xen_broadcast_ipi(XEN_IPI_GENERIC);
322 }
323 }
324
325 static void
326 xen_ipi_hvcb(struct cpu_info *ci, struct intrframe *intrf)
327 {
328 KASSERT(ci != NULL);
329 KASSERT(intrf != NULL);
330 KASSERT(ci == curcpu());
331 KASSERT(!ci->ci_vcpu->evtchn_upcall_mask);
332
333 hypervisor_force_callback();
334 }
335