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