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