Home | History | Annotate | Line # | Download | only in x86
xen_ipi.c revision 1.1.2.1
      1 /* $NetBSD: xen_ipi.c,v 1.1.2.1 2011/06/03 13:27:41 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.1.2.1 2011/06/03 13:27:41 cherry Exp $");
     37  */
     38 
     39 __KERNEL_RCSID(0, "$NetBSD: xen_ipi.c,v 1.1.2.1 2011/06/03 13:27:41 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 #include <machine/fpu.h>
     53 #include <machine/frame.h>
     54 
     55 #include <xen/intr.h>
     56 #include <xen/intrdefs.h>
     57 #include <xen/hypervisor.h>
     58 #include <xen/xen3-public/vcpu.h>
     59 
     60 extern void ddb_ipi(struct trapframe);
     61 
     62 static void xen_ipi_halt(struct cpu_info *, struct intrframe *);
     63 static void xen_ipi_synch_fpu(struct cpu_info *, struct intrframe *);
     64 static void xen_ipi_ddb(struct cpu_info *, struct intrframe *);
     65 static void xen_ipi_xcall(struct cpu_info *, struct intrframe *);
     66 
     67 static void (*ipifunc[XEN_NIPIS])(struct cpu_info *, struct intrframe *) =
     68 {	/* In order of priority (see: xen/include/intrdefs.h */
     69 	xen_ipi_halt,
     70 	xen_ipi_synch_fpu,
     71 	xen_ipi_ddb,
     72 	xen_ipi_xcall
     73 };
     74 
     75 static void
     76 xen_ipi_handler(struct cpu_info *ci, struct intrframe *regs)
     77 {
     78 	uint32_t pending;
     79 	int bit;
     80 
     81 	pending = atomic_swap_32(&ci->ci_ipis, 0);
     82 
     83 	KDASSERT((pending >> XEN_NIPIS) == 0);
     84 	while ((bit = ffs(pending)) != 0) {
     85 		bit--;
     86 		pending &= ~(1 << bit);
     87 		ci->ci_ipi_events[bit].ev_count++;
     88 		if (ipifunc[bit] != NULL) {
     89 			(*ipifunc[bit])(ci, regs);
     90 		}
     91 		else {
     92 			panic("ipifunc[%d] unsupported!\n", bit);
     93 			/* NOTREACHED */
     94 		}
     95 	}
     96 
     97 	return;
     98 }
     99 
    100 /* Must be called once for every cpu that expects to send/recv ipis */
    101 void
    102 xen_ipi_init(void)
    103 {
    104 	cpuid_t vcpu;
    105 	evtchn_port_t evtchn;
    106 	struct cpu_info *ci;
    107 
    108 	ci = curcpu();
    109 
    110 	vcpu = ci->ci_cpuid;
    111 	KASSERT(vcpu < MAX_VIRT_CPUS);
    112 
    113 	evtchn = ci->ci_ipi_evtchn = bind_vcpu_to_evtch(vcpu);
    114 
    115 	KASSERT(evtchn != -1 && evtchn < NR_EVENT_CHANNELS);
    116 
    117 	if (0 != event_set_handler(evtchn, (int (*)(void *))xen_ipi_handler,
    118 				   ci, IPL_HIGH, "ipi")) {
    119 		panic("event_set_handler(...) KPI violation\n");
    120 		/* NOTREACHED */
    121 	}
    122 
    123 	hypervisor_enable_event(evtchn);
    124 	return;
    125 }
    126 
    127 /* prefer this to global variable */
    128 static inline u_int max_cpus(void)
    129 {
    130 	return maxcpus;
    131 }
    132 
    133 static inline bool /* helper */
    134 valid_ipimask(uint32_t ipimask)
    135 {
    136 	uint32_t masks =  XEN_IPI_XCALL | XEN_IPI_DDB |
    137 		XEN_IPI_SYNCH_FPU | XEN_IPI_HALT |
    138 		XEN_IPI_KICK;
    139 
    140 	if (ipimask & ~masks) {
    141 		return false;
    142 	}
    143 	else {
    144 		return true;
    145 	}
    146 
    147 }
    148 
    149 int
    150 xen_send_ipi(struct cpu_info *ci, uint32_t ipimask)
    151 {
    152 	evtchn_port_t evtchn;
    153 
    154 	KASSERT(ci != NULL || ci != curcpu());
    155 
    156 	if (!(ci->ci_flags & CPUF_RUNNING)) {
    157 		return ENOENT;
    158 	}
    159 
    160 	evtchn = ci->ci_ipi_evtchn;
    161 	if (false == valid_ipimask(ipimask)) {
    162 		panic("xen_send_ipi() called with invalid ipimask\n");
    163 		/* NOTREACHED */
    164 	}
    165 
    166 	atomic_or_32(&ci->ci_ipis, ipimask);
    167 	hypervisor_notify_via_evtchn(evtchn);
    168 
    169 	return 0;
    170 }
    171 
    172 void
    173 xen_broadcast_ipi(uint32_t ipimask)
    174 {
    175 	struct cpu_info *ci, *self = curcpu();
    176 	CPU_INFO_ITERATOR cii;
    177 
    178 	if (false == valid_ipimask(ipimask)) {
    179 		panic("xen_broadcast_ipi() called with invalid ipimask\n");
    180 		/* NOTREACHED */
    181 	}
    182 
    183 	/*
    184 	 * XXX-cherry: there's an implicit broadcast sending order
    185 	 * which I dislike. Randomise this ? :-)
    186 	 */
    187 
    188 	for (CPU_INFO_FOREACH(cii, ci)) {
    189 		if (ci == NULL)
    190 			continue;
    191 		if (ci == self)
    192 			continue;
    193 		if (ci->ci_data.cpu_idlelwp == NULL)
    194 			continue;
    195 		if ((ci->ci_flags & CPUF_PRESENT) == 0)
    196 			continue;
    197 		if (ci->ci_flags & (CPUF_RUNNING)) {
    198 			if (0 != xen_send_ipi(ci, ipimask)) {
    199 				panic("xen_ipi of %x from %s to %s failed\n",
    200 				      ipimask, cpu_name(curcpu()),
    201 				      cpu_name(ci));
    202 			}
    203 		}
    204 	}
    205 
    206 	return;
    207 	/* NOTREACHED */
    208 }
    209 
    210 /* MD wrapper for the xcall(9) callback. */
    211 #define PRIuCPUID	"lu" /* XXX: move this somewhere more appropriate */
    212 
    213 static void
    214 xen_ipi_halt(struct cpu_info *ci, struct intrframe *intrf)
    215 {
    216 	KASSERT(ci == curcpu());
    217 	KASSERT(ci != NULL);
    218 	if (HYPERVISOR_vcpu_op(VCPUOP_down, ci->ci_cpuid, NULL)) {
    219 		panic("vcpu%" PRIuCPUID "shutdown failed.\n", ci->ci_cpuid);
    220 	}
    221 
    222 	return;
    223 }
    224 
    225 static void
    226 xen_ipi_synch_fpu(struct cpu_info *ci, struct intrframe *intrf)
    227 {
    228 	KASSERT(ci != NULL);
    229 	KASSERT(intrf != NULL);
    230 
    231 	fpusave_cpu(true);
    232 	return;
    233 }
    234 
    235 static void
    236 xen_ipi_ddb(struct cpu_info *ci, struct intrframe *intrf)
    237 {
    238 	KASSERT(ci != NULL);
    239 	KASSERT(intrf != NULL);
    240 
    241 	ddb_ipi(intrf->if_tf);
    242 }
    243 
    244 static void
    245 xen_ipi_xcall(struct cpu_info *ci, struct intrframe *intrf)
    246 {
    247 	KASSERT(ci != NULL);
    248 	KASSERT(intrf != NULL);
    249 
    250 	xc_ipi_handler();
    251 	return;
    252 }
    253 
    254 void
    255 xc_send_ipi(struct cpu_info *ci)
    256 {
    257 
    258 	KASSERT(kpreempt_disabled());
    259 	KASSERT(curcpu() != ci);
    260 	printf("xc_send_ipi called \n");
    261 	if (ci) {
    262 		if (0 != xen_send_ipi(ci, XEN_IPI_XCALL)) {
    263 			panic("xen_send_ipi(XEN_IPI_XCALL) failed\n");
    264 		};
    265 	} else {
    266 		xen_broadcast_ipi(XEN_IPI_XCALL);
    267 	}
    268 }
    269