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