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