xen_ipi.c revision 1.1.2.2 1 1.1.2.2 cherry /* $NetBSD: xen_ipi.c,v 1.1.2.2 2011/07/31 20:49:12 cherry Exp $ */
2 1.1.2.1 cherry
3 1.1.2.1 cherry /*-
4 1.1.2.1 cherry * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 1.1.2.1 cherry * All rights reserved.
6 1.1.2.1 cherry *
7 1.1.2.1 cherry * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.1 cherry * by Cherry G. Mathew <cherry (at) zyx.in>
9 1.1.2.1 cherry *
10 1.1.2.1 cherry * Redistribution and use in source and binary forms, with or without
11 1.1.2.1 cherry * modification, are permitted provided that the following conditions
12 1.1.2.1 cherry * are met:
13 1.1.2.1 cherry * 1. Redistributions of source code must retain the above copyright
14 1.1.2.1 cherry * notice, this list of conditions and the following disclaimer.
15 1.1.2.1 cherry * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.1 cherry * notice, this list of conditions and the following disclaimer in the
17 1.1.2.1 cherry * documentation and/or other materials provided with the distribution.
18 1.1.2.1 cherry *
19 1.1.2.1 cherry * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.2.1 cherry * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.2.1 cherry * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.2.1 cherry * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.2.1 cherry * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.2.1 cherry * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.2.1 cherry * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.2.1 cherry * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.2.1 cherry * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.2.1 cherry * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.2.1 cherry * POSSIBILITY OF SUCH DAMAGE.
30 1.1.2.1 cherry */
31 1.1.2.1 cherry
32 1.1.2.1 cherry #include <sys/cdefs.h> /* RCS ID macro */
33 1.1.2.1 cherry
34 1.1.2.1 cherry /*
35 1.1.2.1 cherry * Based on: x86/ipi.c
36 1.1.2.2 cherry * __KERNEL_RCSID(0, "$NetBSD: xen_ipi.c,v 1.1.2.2 2011/07/31 20:49:12 cherry Exp $");
37 1.1.2.1 cherry */
38 1.1.2.1 cherry
39 1.1.2.2 cherry __KERNEL_RCSID(0, "$NetBSD: xen_ipi.c,v 1.1.2.2 2011/07/31 20:49:12 cherry Exp $");
40 1.1.2.1 cherry
41 1.1.2.1 cherry #include <sys/types.h>
42 1.1.2.1 cherry
43 1.1.2.1 cherry #include <sys/atomic.h>
44 1.1.2.1 cherry #include <sys/mutex.h>
45 1.1.2.1 cherry #include <sys/cpu.h>
46 1.1.2.1 cherry #include <sys/device.h>
47 1.1.2.1 cherry #include <sys/xcall.h>
48 1.1.2.1 cherry #include <sys/errno.h>
49 1.1.2.1 cherry #include <sys/systm.h>
50 1.1.2.1 cherry
51 1.1.2.1 cherry #include <machine/cpu.h>
52 1.1.2.2 cherry #ifdef __x86_64__
53 1.1.2.1 cherry #include <machine/fpu.h>
54 1.1.2.2 cherry #else
55 1.1.2.2 cherry #include <machine/npx.h>
56 1.1.2.2 cherry #endif /* __x86_64__ */
57 1.1.2.1 cherry #include <machine/frame.h>
58 1.1.2.2 cherry #include <machine/segments.h>
59 1.1.2.1 cherry
60 1.1.2.1 cherry #include <xen/intr.h>
61 1.1.2.1 cherry #include <xen/intrdefs.h>
62 1.1.2.1 cherry #include <xen/hypervisor.h>
63 1.1.2.1 cherry #include <xen/xen3-public/vcpu.h>
64 1.1.2.1 cherry
65 1.1.2.2 cherry #ifdef __x86_64__
66 1.1.2.1 cherry extern void ddb_ipi(struct trapframe);
67 1.1.2.2 cherry #else
68 1.1.2.2 cherry extern void ddb_ipi(int, struct trapframe);
69 1.1.2.2 cherry #endif /* __x86_64__ */
70 1.1.2.1 cherry
71 1.1.2.1 cherry static void xen_ipi_halt(struct cpu_info *, struct intrframe *);
72 1.1.2.1 cherry static void xen_ipi_synch_fpu(struct cpu_info *, struct intrframe *);
73 1.1.2.1 cherry static void xen_ipi_ddb(struct cpu_info *, struct intrframe *);
74 1.1.2.1 cherry static void xen_ipi_xcall(struct cpu_info *, struct intrframe *);
75 1.1.2.1 cherry
76 1.1.2.1 cherry static void (*ipifunc[XEN_NIPIS])(struct cpu_info *, struct intrframe *) =
77 1.1.2.1 cherry { /* In order of priority (see: xen/include/intrdefs.h */
78 1.1.2.1 cherry xen_ipi_halt,
79 1.1.2.1 cherry xen_ipi_synch_fpu,
80 1.1.2.1 cherry xen_ipi_ddb,
81 1.1.2.1 cherry xen_ipi_xcall
82 1.1.2.1 cherry };
83 1.1.2.1 cherry
84 1.1.2.1 cherry static void
85 1.1.2.1 cherry xen_ipi_handler(struct cpu_info *ci, struct intrframe *regs)
86 1.1.2.1 cherry {
87 1.1.2.1 cherry uint32_t pending;
88 1.1.2.1 cherry int bit;
89 1.1.2.1 cherry
90 1.1.2.1 cherry pending = atomic_swap_32(&ci->ci_ipis, 0);
91 1.1.2.1 cherry
92 1.1.2.1 cherry KDASSERT((pending >> XEN_NIPIS) == 0);
93 1.1.2.1 cherry while ((bit = ffs(pending)) != 0) {
94 1.1.2.1 cherry bit--;
95 1.1.2.1 cherry pending &= ~(1 << bit);
96 1.1.2.1 cherry ci->ci_ipi_events[bit].ev_count++;
97 1.1.2.1 cherry if (ipifunc[bit] != NULL) {
98 1.1.2.1 cherry (*ipifunc[bit])(ci, regs);
99 1.1.2.1 cherry }
100 1.1.2.1 cherry else {
101 1.1.2.1 cherry panic("ipifunc[%d] unsupported!\n", bit);
102 1.1.2.1 cherry /* NOTREACHED */
103 1.1.2.1 cherry }
104 1.1.2.1 cherry }
105 1.1.2.1 cherry
106 1.1.2.1 cherry return;
107 1.1.2.1 cherry }
108 1.1.2.1 cherry
109 1.1.2.1 cherry /* Must be called once for every cpu that expects to send/recv ipis */
110 1.1.2.1 cherry void
111 1.1.2.1 cherry xen_ipi_init(void)
112 1.1.2.1 cherry {
113 1.1.2.1 cherry cpuid_t vcpu;
114 1.1.2.1 cherry evtchn_port_t evtchn;
115 1.1.2.1 cherry struct cpu_info *ci;
116 1.1.2.1 cherry
117 1.1.2.1 cherry ci = curcpu();
118 1.1.2.1 cherry
119 1.1.2.1 cherry vcpu = ci->ci_cpuid;
120 1.1.2.1 cherry KASSERT(vcpu < MAX_VIRT_CPUS);
121 1.1.2.1 cherry
122 1.1.2.1 cherry evtchn = ci->ci_ipi_evtchn = bind_vcpu_to_evtch(vcpu);
123 1.1.2.1 cherry
124 1.1.2.1 cherry KASSERT(evtchn != -1 && evtchn < NR_EVENT_CHANNELS);
125 1.1.2.1 cherry
126 1.1.2.1 cherry if (0 != event_set_handler(evtchn, (int (*)(void *))xen_ipi_handler,
127 1.1.2.1 cherry ci, IPL_HIGH, "ipi")) {
128 1.1.2.1 cherry panic("event_set_handler(...) KPI violation\n");
129 1.1.2.1 cherry /* NOTREACHED */
130 1.1.2.1 cherry }
131 1.1.2.1 cherry
132 1.1.2.1 cherry hypervisor_enable_event(evtchn);
133 1.1.2.1 cherry return;
134 1.1.2.1 cherry }
135 1.1.2.1 cherry
136 1.1.2.1 cherry /* prefer this to global variable */
137 1.1.2.1 cherry static inline u_int max_cpus(void)
138 1.1.2.1 cherry {
139 1.1.2.1 cherry return maxcpus;
140 1.1.2.1 cherry }
141 1.1.2.1 cherry
142 1.1.2.1 cherry static inline bool /* helper */
143 1.1.2.1 cherry valid_ipimask(uint32_t ipimask)
144 1.1.2.1 cherry {
145 1.1.2.1 cherry uint32_t masks = XEN_IPI_XCALL | XEN_IPI_DDB |
146 1.1.2.1 cherry XEN_IPI_SYNCH_FPU | XEN_IPI_HALT |
147 1.1.2.1 cherry XEN_IPI_KICK;
148 1.1.2.1 cherry
149 1.1.2.1 cherry if (ipimask & ~masks) {
150 1.1.2.1 cherry return false;
151 1.1.2.1 cherry }
152 1.1.2.1 cherry else {
153 1.1.2.1 cherry return true;
154 1.1.2.1 cherry }
155 1.1.2.1 cherry
156 1.1.2.1 cherry }
157 1.1.2.1 cherry
158 1.1.2.1 cherry int
159 1.1.2.1 cherry xen_send_ipi(struct cpu_info *ci, uint32_t ipimask)
160 1.1.2.1 cherry {
161 1.1.2.1 cherry evtchn_port_t evtchn;
162 1.1.2.1 cherry
163 1.1.2.1 cherry KASSERT(ci != NULL || ci != curcpu());
164 1.1.2.1 cherry
165 1.1.2.1 cherry if (!(ci->ci_flags & CPUF_RUNNING)) {
166 1.1.2.1 cherry return ENOENT;
167 1.1.2.1 cherry }
168 1.1.2.1 cherry
169 1.1.2.1 cherry evtchn = ci->ci_ipi_evtchn;
170 1.1.2.1 cherry if (false == valid_ipimask(ipimask)) {
171 1.1.2.1 cherry panic("xen_send_ipi() called with invalid ipimask\n");
172 1.1.2.1 cherry /* NOTREACHED */
173 1.1.2.1 cherry }
174 1.1.2.1 cherry
175 1.1.2.1 cherry atomic_or_32(&ci->ci_ipis, ipimask);
176 1.1.2.1 cherry hypervisor_notify_via_evtchn(evtchn);
177 1.1.2.1 cherry
178 1.1.2.1 cherry return 0;
179 1.1.2.1 cherry }
180 1.1.2.1 cherry
181 1.1.2.1 cherry void
182 1.1.2.1 cherry xen_broadcast_ipi(uint32_t ipimask)
183 1.1.2.1 cherry {
184 1.1.2.1 cherry struct cpu_info *ci, *self = curcpu();
185 1.1.2.1 cherry CPU_INFO_ITERATOR cii;
186 1.1.2.1 cherry
187 1.1.2.1 cherry if (false == valid_ipimask(ipimask)) {
188 1.1.2.1 cherry panic("xen_broadcast_ipi() called with invalid ipimask\n");
189 1.1.2.1 cherry /* NOTREACHED */
190 1.1.2.1 cherry }
191 1.1.2.1 cherry
192 1.1.2.1 cherry /*
193 1.1.2.1 cherry * XXX-cherry: there's an implicit broadcast sending order
194 1.1.2.1 cherry * which I dislike. Randomise this ? :-)
195 1.1.2.1 cherry */
196 1.1.2.1 cherry
197 1.1.2.1 cherry for (CPU_INFO_FOREACH(cii, ci)) {
198 1.1.2.1 cherry if (ci == NULL)
199 1.1.2.1 cherry continue;
200 1.1.2.1 cherry if (ci == self)
201 1.1.2.1 cherry continue;
202 1.1.2.1 cherry if (ci->ci_data.cpu_idlelwp == NULL)
203 1.1.2.1 cherry continue;
204 1.1.2.1 cherry if ((ci->ci_flags & CPUF_PRESENT) == 0)
205 1.1.2.1 cherry continue;
206 1.1.2.1 cherry if (ci->ci_flags & (CPUF_RUNNING)) {
207 1.1.2.1 cherry if (0 != xen_send_ipi(ci, ipimask)) {
208 1.1.2.1 cherry panic("xen_ipi of %x from %s to %s failed\n",
209 1.1.2.1 cherry ipimask, cpu_name(curcpu()),
210 1.1.2.1 cherry cpu_name(ci));
211 1.1.2.1 cherry }
212 1.1.2.1 cherry }
213 1.1.2.1 cherry }
214 1.1.2.1 cherry
215 1.1.2.1 cherry return;
216 1.1.2.1 cherry /* NOTREACHED */
217 1.1.2.1 cherry }
218 1.1.2.1 cherry
219 1.1.2.1 cherry /* MD wrapper for the xcall(9) callback. */
220 1.1.2.1 cherry #define PRIuCPUID "lu" /* XXX: move this somewhere more appropriate */
221 1.1.2.1 cherry
222 1.1.2.1 cherry static void
223 1.1.2.1 cherry xen_ipi_halt(struct cpu_info *ci, struct intrframe *intrf)
224 1.1.2.1 cherry {
225 1.1.2.1 cherry KASSERT(ci == curcpu());
226 1.1.2.1 cherry KASSERT(ci != NULL);
227 1.1.2.1 cherry if (HYPERVISOR_vcpu_op(VCPUOP_down, ci->ci_cpuid, NULL)) {
228 1.1.2.1 cherry panic("vcpu%" PRIuCPUID "shutdown failed.\n", ci->ci_cpuid);
229 1.1.2.1 cherry }
230 1.1.2.1 cherry
231 1.1.2.1 cherry return;
232 1.1.2.1 cherry }
233 1.1.2.1 cherry
234 1.1.2.1 cherry static void
235 1.1.2.1 cherry xen_ipi_synch_fpu(struct cpu_info *ci, struct intrframe *intrf)
236 1.1.2.1 cherry {
237 1.1.2.1 cherry KASSERT(ci != NULL);
238 1.1.2.1 cherry KASSERT(intrf != NULL);
239 1.1.2.1 cherry
240 1.1.2.2 cherry #ifdef __x86_64__
241 1.1.2.1 cherry fpusave_cpu(true);
242 1.1.2.2 cherry #else
243 1.1.2.2 cherry npxsave_cpu(true);
244 1.1.2.2 cherry #endif /* __x86_64__ */
245 1.1.2.1 cherry return;
246 1.1.2.1 cherry }
247 1.1.2.1 cherry
248 1.1.2.1 cherry static void
249 1.1.2.1 cherry xen_ipi_ddb(struct cpu_info *ci, struct intrframe *intrf)
250 1.1.2.1 cherry {
251 1.1.2.1 cherry KASSERT(ci != NULL);
252 1.1.2.1 cherry KASSERT(intrf != NULL);
253 1.1.2.1 cherry
254 1.1.2.2 cherry #ifdef __x86_64__
255 1.1.2.1 cherry ddb_ipi(intrf->if_tf);
256 1.1.2.2 cherry #else
257 1.1.2.2 cherry struct trapframe tf;
258 1.1.2.2 cherry tf.tf_gs = intrf->if_gs;
259 1.1.2.2 cherry tf.tf_fs = intrf->if_fs;
260 1.1.2.2 cherry tf.tf_es = intrf->if_es;
261 1.1.2.2 cherry tf.tf_ds = intrf->if_ds;
262 1.1.2.2 cherry tf.tf_edi = intrf->if_edi;
263 1.1.2.2 cherry tf.tf_esi = intrf->if_esi;
264 1.1.2.2 cherry tf.tf_ebp = intrf->if_ebp;
265 1.1.2.2 cherry tf.tf_ebx = intrf->if_ebx;
266 1.1.2.2 cherry tf.tf_ecx = intrf->if_ecx;
267 1.1.2.2 cherry tf.tf_eax = intrf->if_eax;
268 1.1.2.2 cherry tf.tf_trapno = intrf->__if_trapno;
269 1.1.2.2 cherry tf.tf_err = intrf->__if_err;
270 1.1.2.2 cherry tf.tf_eip = intrf->if_eip;
271 1.1.2.2 cherry tf.tf_cs = intrf->if_cs;
272 1.1.2.2 cherry tf.tf_eflags = intrf->if_eflags;
273 1.1.2.2 cherry tf.tf_esp = intrf->if_esp;
274 1.1.2.2 cherry tf.tf_ss = intrf->if_ss;
275 1.1.2.2 cherry
276 1.1.2.2 cherry /* XXX: does i386/Xen have vm86 support ?
277 1.1.2.2 cherry tf.tf_vm86_es;
278 1.1.2.2 cherry tf.tf_vm86_ds;
279 1.1.2.2 cherry tf.tf_vm86_fs;
280 1.1.2.2 cherry tf.tf_vm86_gs;
281 1.1.2.2 cherry :XXX */
282 1.1.2.2 cherry
283 1.1.2.2 cherry ddb_ipi(SEL_KPL, tf);
284 1.1.2.2 cherry #endif
285 1.1.2.1 cherry }
286 1.1.2.1 cherry
287 1.1.2.1 cherry static void
288 1.1.2.1 cherry xen_ipi_xcall(struct cpu_info *ci, struct intrframe *intrf)
289 1.1.2.1 cherry {
290 1.1.2.1 cherry KASSERT(ci != NULL);
291 1.1.2.1 cherry KASSERT(intrf != NULL);
292 1.1.2.1 cherry
293 1.1.2.1 cherry xc_ipi_handler();
294 1.1.2.1 cherry return;
295 1.1.2.1 cherry }
296 1.1.2.1 cherry
297 1.1.2.1 cherry void
298 1.1.2.1 cherry xc_send_ipi(struct cpu_info *ci)
299 1.1.2.1 cherry {
300 1.1.2.1 cherry
301 1.1.2.1 cherry KASSERT(kpreempt_disabled());
302 1.1.2.1 cherry KASSERT(curcpu() != ci);
303 1.1.2.1 cherry printf("xc_send_ipi called \n");
304 1.1.2.1 cherry if (ci) {
305 1.1.2.1 cherry if (0 != xen_send_ipi(ci, XEN_IPI_XCALL)) {
306 1.1.2.1 cherry panic("xen_send_ipi(XEN_IPI_XCALL) failed\n");
307 1.1.2.1 cherry };
308 1.1.2.1 cherry } else {
309 1.1.2.1 cherry xen_broadcast_ipi(XEN_IPI_XCALL);
310 1.1.2.1 cherry }
311 1.1.2.1 cherry }
312