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